连载3:利用PHP创建由Oracle驱动的SOAP服务,连载3:利用PHP创建由Oracle驱动的SOAP服务
【 tulaoshi.com - PHP 】
《连载1:利用PHP创建由Oracle驱动的SOAP服务》
《连载2:利用PHP创建由Oracle 驱动的SOAP 服务》
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)为便于参考,下面是一个完整的 BookManager 类,以及使用该类公开 SOAP 服务的相应服务器脚本。
<?php
/**
* SOAP Service class to manage the books table
*
* @author John Coggeshall <john@zend.com
*
* @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
来源:http://www.tulaoshi.com/n/20160129/1488205.html
看过《连载3:利用PHP创建由Oracle驱动的SOAP服务》的人还看了以下文章 更多>>