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

PHP
PHP技巧:详解phplib模板使用过程及运行原理
PHP技巧:Smarty+adodb分页示例
PHP技巧:PHP脚本中关于拼写检查函数库
PHP技巧:PHP脚本编程中的文件系统函数库
PHP技巧:PHP中几种删除目录的三种方法
学习PHP技术:txtSQL安装手册中文版
学习PHP:PHP的通用检测函数总结
详细学习PHP中对文件和目录的操作方法
PHP+MYSQL实例:网站在线人数的程序代码
Linux操作系统启动httpd失败的解决方法
初学:在PHP开发中如何使用Session?
PHP初学者遇到的中文乱码解决方案
PHP实例:实现文件上传的程序源码
PHP实例:常用的数值判断函数
PHP实例源代码:PHP实现翻页处理的类
PHP实例:从数组里筛选出重复的数据
PHP实例:用PHP实现windows风格的树型菜单
PHP实例程序:实现给上传图片加水印图案的做法
PHP实例:用PHP编写的网上调查投票系统
PHP实例:一个非常全面获取图象信息的PHP函数

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


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