当前位置: 首页 > 图文教程 > 网络编程 > PHP > 连载3:利用PHP创建由Oracle驱动的SOAP服务

PHP
PHP中for循环语句的几种“变态”用法
用PHP与XML联手进行网站开发
PHP程序漏洞产生的原因和防范方法
利用PHP编程防范XSS跨站脚本攻击
使用PHP往Windows系统中添加用户
PHP Shell的编写(改进版)
PHP开发中接收复选框信息的方法
PHP程序加速探索之服务器负载测试
PHP实现首页自动选择语言转跳
十天学会php之第一天
十天学会php之第二天
十天学会php之第三天
十天学会php之第四天
十天学会php之第五天
十天学会php之第六天
十天学会php之第七天
十天学会php之第八天
十天学会php之第九天
十天学会php之第十天
Web开发源代码:PHP生成静态页面的类

连载3:利用PHP创建由Oracle驱动的SOAP服务


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 28 ::
收藏到网摘: n/a

《连载1:利用PHP创建由Oracle驱动的SOAP服务》

《连载2:利用PHP创建由Oracle 驱动的SOAP 服务》

为便于参考,下面是一个完整的 BookManager 类,以及使用该类公开 SOAP 服务的相应服务器脚本。

 <?php

/**
 * SOAP Service class to manage the books table
 *
 * @author John Coggeshall <[email protected]>
 *
 * @throws SoapFault
 */
class BookManager {
 
 private $objDB;

 const DB_USERNAME="demo";
 const DB_PASSWORD="password";
 const DB_DATABASE="myoracle";
 
 /**
  * Object Constructor: Establishes DB connection
  *
  */
 function __construct() {
  
  $this->objDB = oci_connect(self::DB_USERNAME,
                             self::DB_PASSWORD,
                             self::DB_DATABASE);
      
  if($this->objDB === false) {
   throw new SoapFault(-1, "Failed to connect to database backend (reason: " .
                                         oci_error() . ")");
  }
 }

 /**
  * Private method to return the DB connection and make sure it exists
  *
  * @return unknown
  */
 private function getDB() {
  if(!$this->objDB) {
   throw new SoapFault(-1, "No valid database connection");
  }
  
  return $this->objDB;
 }
 
 /**
  * Add a new book to the database
  *
  * @param string $isbn The ISBN serial number for the book (32 char max)
  * @param string $author The name of the primary author (50 char max)
  * @param string $title The title of the book (50 char max)
  * @param float $price The price of the book in USD
  *
  * @return mixed SOAP Fault on error, true on success
  */
 public function addBook($isbn, $author, $title, $price) {

  $query = "INSERT INTO books (isbn, author, title, price)
                 VALUES (:isbn, :author, :title, :price)";
  
  $stmt = oci_parse($this->getDB(), $query);
  
  if(!$stmt) {
   throw new SoapFault(-1, "Failed to prepare query (reason: "  .
                                         oci_error($stmt) . ")");
  }
  
  // The numbers 32, 50, 50 are the max column lengths
  oci_bind_by_name($stmt, "isbn", $isbn, 32);
  oci_bind_by_name($stmt, "author", $author, 50);
  oci_bind_by_name($stmt, "title", $title, 50);
  oci_bind_by_name($stmt, "price", $price);
  
  if(!oci_execute($stmt)) {
   oci_rollback($this->getDB());
   throw new SoapFault(-1, "Failed to execute query (reason: " .
                                     &nbs