note 101979 modified in class.soapserver by danbrown
| From: | danbrown@php.net | Date: | Fri, 21 Jan 2011 15:25:17 +0000 |
| Subject: | note 101979 modified in class.soapserver by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175964@lists.php.net to get a copy of this message | ||
Another simple example for SOAP_SERVER with errorhandling an params and wsdl:
SERVER (soap_all_srv.php):
<?php
// PEAR::SOAP einbinden
require_once "SOAP/Server.php";
$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
$skiptrace = true;
// Service-Class
class mytimeserv {
// __dispatch_map
public $__dispatch_map = array ();
// In/Out param -> __dispatch_map
public function __construct() {
$this->__dispatch_map["now"] =
array ("in" => array("format" => "string"),
"out" => array("time" => "string"));
}
// get back __dispatch_map in __dispatch
public function __dispatch($methodname) {
if (isset($this->__dispatch_map[$methodname])) {
return $this->__dispatch_map[$methodname];
}
return NULL;
}
// servicemthod with parameters
function now ($format) {
// formaterror?
if (($format == null) || (trim($format) == "")) {
// send errormessage
return new SOAP_Fault("Kein Parameter angegeben","0815",
"Client");
}
date_default_timezone_set('Europe/Berlin');
$time = date ($format);
// return SOAP-Obj.
return (new SOAP_Value('time','string', $time));
}
}
// service-class
$service = new mytimeserv();
// server
$ss = new SOAP_Server();
// add service with name
$ss->addObjectMap (&$service,"urn:mytimeserv");
// 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,"mytimeserv","My Time Service");
// set HTML-Header
header("Content-type: text/xml");
// return wsdl
print $disco->getWSDL ();
}
}
?>
CLIENT (soap_all_client.php) (for wsdl: http://example.com/soap_all_srv.php?wsdl):
<?php
require_once "SOAP/Client.php";
// SOAP/WSDL
$sw = new SOAP_WSDL ("http://example.com/soap_all_srv.php?wsdl");
// Proxy-Obj.
$proxy = $sw->getProxy ();
// servicemthod
$erg = $proxy->now ("H:i:s");
// return
print $erg."\n";
?>
--was--
Another simple example for SOAP_SERVER with errorhandling an params and wsdl:
SERVER (soap_all_srv.php):
<?php
// PEAR::SOAP einbinden
require_once "SOAP/Server.php";
$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
$skiptrace = true;
// Service-Class
class mytimeserv {
// __dispatch_map
public $__dispatch_map = array ();
// In/Out param -> __dispatch_map
public function __construct() {
$this->__dispatch_map["now"] =
array ("in" => array("format" => "string"),
"out" => array("time" => "string"));
}
// get back __dispatch_map in __dispatch
public function __dispatch($methodname) {
if (isset($this->__dispatch_map[$methodname])) {
return $this->__dispatch_map[$methodname];
}
return NULL;
}
// servicemthod with parameters
function now ($format) {
// formaterror?
if (($format == null) || (trim($format) == "")) {
// send errormessage
return new SOAP_Fault("Kein Parameter angegeben","0815",
"Client");
}
date_default_timezone_set('Europe/Berlin');
$time = date ($format);
// return SOAP-Obj.
return (new SOAP_Value('time','string', $time));
}
}
// service-class
$service = new mytimeserv();
// server
$ss = new SOAP_Server();
// add service with name
$ss->addObjectMap (&$service,"urn:mytimeserv");
// 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,"mytimeserv","My Time Service");
// set HTML-Header
header("Content-type: text/xml");
// return wsdl
print $disco->getWSDL ();
}
}
?>
CLIENT (soap_all_client.php) (for wsdl: http://192.168.178.99/soap_all_srv.php?wsdl):
<?php
require_once "SOAP/Client.php";
// SOAP/WSDL
$sw = new SOAP_WSDL ("http://192.168.178.99/soap_all_srv.php?wsdl");
// Proxy-Obj.
$proxy = $sw->getProxy ();
// servicemthod
$erg = $proxy->now ("H:i:s");
// return
print $erg."\n";
?>
http://php.net/manual/en/class.soapserver.php