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

PHP
用PHP实现Ftp用户的在线管理
用PHP实现分段下载
最令PHP初学者头痛的十四个问题
用PHP写的MD5加密函数
PHP应用程序加速探索之简介
将SSH与PHP相连接 确保传输数据的安全
PHP制作的仿百度的站内搜索引擎代码
PHP读取汉字点阵数据
PHP实现任意字符集下正常显示网页的方法
利用PHP的OOP特性实现数据保护
关于PHP字符集的问题
新手入门:IIS6环境下的PHP最佳配置方法
新手入门:初学动态网页PHP的18个例子
基于PHP的AJAX技术实现文件异步上传
PHP技巧--通过COM使用ADODB
PHP技巧:正确理解PHP程序编译时的错误信息
PHP技巧:分析利用PHP制作新闻系统的步骤
PHP技巧:通过实例深入剖析require和include的用法
PHP技巧:优化动态网页技术PHP程序的12条技巧
PHP技巧:使用APC缓存优化PHP程序

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 26 ::
收藏到网摘: 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