test param:1102
| From: | Mai Levesque | Date: | Wed, 25 Jan 2012 20:50:45 +0000 |
| Subject: | test param:1102 | ||
| Groups: | php.test | ||
| Request: | Send a blank email to php-test+get-918@lists.php.net to get a copy of this message | ||
PEAR::SOAP Web Service:
Zugriff auf eine Weindatenbank
--------------------------------------------------------------------------------
Client
--------------------------------------------------------------------------------
<?php
// Enable Error Reporting
ini_set('display_errors',1);
// bind SOAP/Client.php -> path of the php file
require_once "SOAP/Client.php";
// SOAP/WSDL
$sw = new SOAP_WSDL ("http://localhost/server.php?wsdl");
// Proxy-Obj.
$proxy = $sw->getProxy ();
// servicemethod 1
$erg = $proxy->vollTextSuche ("%riesling%");
// return result 1
reset($erg);
while( key($erg) !== NULL )
{
echo current($erg)."\n";
next($erg);
}
// servicemethod 2
$erg = $proxy->holeNamePreis ("3");
// return result 2
reset($erg);
while( key($erg) !== NULL )
{
echo current($erg)." ";
next($erg);
}
?>
--------------------------------------------------------------------------------
Server
--------------------------------------------------------------------------------
<?php
// Enable Error Reporting
ini_set('display_errors',1);
// bind PEAR::SOAP
require_once "SOAP/Server.php";
$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
$skiptrace = true;
// Database-Class
class mysqldb {
private $db;
public function __construct() {
// Connect to mysql server and select database
try {
$this->db = new PDO ("mysql:host=localhost;dbname=database","user","password");
} catch (PDOException $e) {
throw new Exception ('Connection failed: ' .$e->getMessage());
}
}
public function getID ($name) {
// Get data from wein
$a = array();
if (!$stmt=$this->db->prepare('SELECT wid FROM wein WHERE weinname LIKE :name'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':name',$name))
throw new Exception ('PDOStatement::bindParam failed!');
if (!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
// Save data to an array
while($wein=$stmt->fetchObject()) {
array_push($a, $wein->wid);
}
// Return result
return ($a);
}
public function getNameValue ($id) {
// Get data from wein
$a = array();
if (!$stmt=$this->db->prepare('SELECT weinname,preis FROM wein WHERE wid=:id'))
throw new Exception ('PDOStatement::prepare failed!');
if (!$stmt->bindParam(':id',$id))
throw new Exception ('PDOStatement::bindParam failed!');
if (!$stmt->execute())
throw new Exception ('PDOStatement::execute failed!');
// Save data to an array
while($wein=$stmt->fetchObject()) {
array_push($a, $wein->weinname);
array_push($a, $wein->preis);
}
// Return result
return ($a);
}
}
// Service-Class
class mywineserv {
// __dispatch_map
public $__dispatch_map = array ();
// In/Out param -> __dispatch_map
public function __construct() {
$this->__dispatch_map["vollTextSuche"] =
array ("in" => array("name" => "string"),
"out" => array("id" => "string"));
$this->__dispatch_map["holeNamePreis"] =
array ("in" => array("id" => "string"),
"out" => array("namepreis" => "string"));
}
// get back __dispatch_map in __dispatch
public function __dispatch($methodname) {
if (isset($this->__dispatch_map[$methodname])) {
return $this->__dispatch_map[$methodname];
}
return NULL;} // servicemethod with parameters function vollTextSuche ($name) {
// parameter error?
if (($name == null) || (trim($name) == "")) {
// send errormessage
return new SOAP_Fault("Parameter error","0815", "Client");
}
// Create new db instance
$mysql = new mysqldb();
// Get data from database
$id = array();
$id = $mysql->getID($name);
// return SOAP-Obj.
return (new SOAP_Value('id','string', $id));
}
function holeNamePreis ($id) {
// parameter error?
if (($id == null) || (trim($id) == "")) {
// send errormessage
return new SOAP_Fault("Parameter error","0815", "Client");
}
// Create new db instance
$mysql = new mysqldb();
// Get data from database
$namepreis = array();
$namepreis = $mysql->getNameValue($id);
// return SOAP-Obj.
return (new SOAP_Value('namepreis','string', $namepreis));
}
}
// service-class
$service = new mywineserv();
// server
$ss = new SOAP_Server();
// add service with name
$ss->addObjectMap (&$service,"urn:mywineserv");
// service or wsdl
if (isset($_SERVER["REQUEST_METHOD"])&& $_SERVER["REQUEST_METHOD"] == "POST") {
// postdata -> service
$ss->service ($HTTP_RAW_POST_DATA);
} else {
// wsdl-param in url
if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl') == 0) {
// DISCO_Server for WSDL
require_once "SOAP/Disco.php";
$disco = new SOAP_DISCO_Server ($ss,"mywineserv","My Wine Service");
// set HTML-Header
header("Content-type: text/xml");
// return wsdl
print $disco->getWSDL ();
}
}
?>