An overview of Soap
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope xmlns:soap="http://shcemas.xmlsoap.org/soap/envelope/" >
<soap:Body>
<getTime xmlns="http://www.phpbuilder.com/adam_delves/fourth_dimension/">
<timeZone>London/Europe</timeZone>
</getTime>
</soap:Body>
</soap:Envelope>
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope xmlns:soap="http://shcemas.xmlsoap.org/soap/envelope/" >
<soap:Body>
<getTimeResponse xmlns="http://www.phpbuilder.com/columns/adam_delves/fourth_dimension/">
<time>
<hour>12</hour>
<minute>34</minute>
<second>10</second>
</time>
<format>24h</format>
</getTimeResponse>
</soap:Body>
</soap:Envelope>
A SOAP RPC Call
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s-enc="http://schemas.xmlsoap.org/soap/encoding/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding" >
<soap:Body>
<verify xmlns="http://www.phpbuilder.com/columns/adam_delves/email_validator.verify/">
<symbol xsi:type="xsd:integer">54</symbol>
<symbol xsi:type="xsd:string">RG5H4</symbol>
</verify>
</soap:Body>
</soap:Envelope>
<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope
xmlns:soap="http://shcemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s-enc="http://schemas.xmlsoap.org/soap/encoding/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding" >
<soap:Body>
<verifyResponse xmlns="urn:webservices-email_validator">
<Result xsi:type="xsd:boolean">false</Reponse>
</verifyResponse>
</soap:Body>
</soap:Envelope>
- http://shcemas.xmlsoap.org/soap/envelope/ – is the XML Schema language. This contains the basic data types.
- http://www.w3.org/2001/XMLSchema – Used in xsi:type attributes to specify the data type.
- http://schemas.xmlsoap.org/soap/encoding/ – SOAP specific type encodings.
- http://www.phpbuilder.com/columns/adam_delves/email_validator.verify/ - this URI does not exist but serves to identify the name space for the verify method of the email validator
Using Soap in PHP 5
Installation
Windows
extension=php_soap.dll
If the php_soap.dll file is not present on your system, download the latest Windows version of PHP distributed as a ZIP file from the PHP web site.
Unix
# wget ftp://xmlsoft.org/libxml2/libxml2-2.6.11.tar.gz
# tar xzvf libxml2-2.6.11.tar.gz
# cd libxml2-2.6.11
# ./configure
# make
# su -c "make install"
# wget http://uk2.php.net/get/php-5.1.4.tar.gz/from/uk.php.net/mirror
# tar zxvf php-5.1.4.tar.gz
# cd php-5.1.4
# ./configure --enable-soap
# make
# su -c "make install"
PHP:
$params->AWSAccessKeyId = AMAZON_API_KEY;
$params->Request->SearchIndex = 'Books';
$params->Request->Keywords = 'php5 oop';
$amazon = new SoapClient('http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl');
$result = $amazon->itemSearch($params);
- The Amazon web service API's require the parameters to be sent in the form of an object. This is initialized with all the appropriate values first.
- First an instance of the SoapClient object is created using the URI of the WSDL file.
- The itemSearch() operation which is now a method of the SoapClient object is called. This causes PHP to make an HTTP request to the web service, sending a SOAP envelope containing the RPC call.
- The return value of the itemSearch operation sent back from the web service is returned by the itemSearch() method and is decoded into native PHP types automatically.
- The result, like the parameter passed to the function, is in the form of an object. If the request is successful, the Item property will contain an array of Item objects for the items found.
Using the Proxy Generator Class
PHP wsdl_proxy_generator.php:
class WSDLProxyGenerator
{
/**
* Creates an instance of a SoapClient object and all necessary suporting types.
*
*/
public static function createSoapClient($wsdlUri, $clientName='WsdlGerneratedWebService')
{
/* create the SoapClient object using the wsdl file */
$soap = new SoapClient($wsdlUri);
$types = array();
foreach($soap->__getTypes() as $type) {
/* match the type information using a regualr expession */
preg_match("/([a-z0-9_]+)\s+([a-z0-9_]+(\[\])?)(.*)?/si", $type, $matches);
$type = $matches[1];
switch($type) {
/* if the data type is struct, we create a class with this name */
case 'struct':
/* the PHP class name will be ClientName_WebServiceName */
$className = $clientName . '_' . $name;
/* store the data type information in an array for later use in the classmap */
$types[$name] = $className;
/* check the class does not exsits before creating it */
if (! class_exists($className)) {
eval("class $className {}");
}
break;
}
}
/* create another instance of the SoapClient, this time using the classmap */
return new SoapClient($wsdlUri, array('classmap' => $types));
}
/* this class cannot be instantiated as an object */
private function __construct()
{
}
}
[ Next Page ]
Comments: | ||
Very good post | Anup Khandelwal | 09/05/11 05:47 |
Test comment | Anup Khandelwal | 09/05/11 05:33 |
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. |