The following example shows a remote call to a method "answerMe" which takes one parameter and returns a string to the client. Server and client communicate by using a WSDL file, which can be publicly accessed by the client. The WSDL file describes the method (called "operation" in the WSDL file), the data type of the input/output parameters (called "message parts"), and the details of how this is transmitted (using "soap" and "rpc" via the http protocol).
<?php class WorldService { private $worldattributes = array("1" => "brave", "2" => "new"); function answerMe($whatKindOfWorld) { return "Hello ".$this->worldattributes[$whatKindOfWorld]." World"; } } ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache $server = new SoapServer("helloworld.wsdl"); $server->setClass("WorldService"); $server->handle(); ?>
<?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='HelloWorld' targetNamespace='http://example.org/HelloWorld' xmlns:tns=' http://example.org/HelloWorld ' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns='http://schemas.xmlsoap.org/wsdl/'> <message name='getHelloWorldRequest'> <part name='whatKindOfWorld' type='xsd:string'/> </message> <message name='getHelloWorldResponse'> <part name='Result' type='xsd:string'/> </message> <portType name='HelloWorldPortType'> <operation name='answerMe'> <input message='tns:getHelloWorldRequest'/> <output message='tns:getHelloWorldResponse'/> </operation> </portType> <binding name='HelloWorldBinding' type='tns:HelloWorldPortType'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> <operation name='answerMe'> <soap:operation soapAction='urn:localhost-HelloWorld#answerMe'/> <input> <soap:body use='encoded' namespace='urn:localhost-HelloWorld' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </input> <output> <soap:body use='encoded' namespace='urn:localhost-HelloWorld' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </output> </operation> </binding> <service name='HelloWorldService'> <port name='HelloWorldPort' binding='HelloWorldBinding'> <soap:address location='http://YOURSERVER/~USERNAME/php/helloserver.php'/> </port> </service> </definitions>
Test your wsdl file using a generic client.
<?php $client = new SoapClient(NULL, array( "location" => "http://YOURSERVER/~USERNAME/php/helloserver.php", "uri" => "something", "style" => SOAP_RPC, "use" => SOAP_ENCODED )); print($client->__soapCall("answerMe", array(new SoapParam("2","whatKindOfWorld")))); ?>
<?php ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache try { $client = new SoapClient("helloworld.wsdl"); $result = $client->answerMe("1"); print($result); } catch (SoapFault $exception) { echo $exception; } ?>
5) Display the SOAP XML data:
If you change the client constructor to
$client = new SoapClient("helloworld.wsdl",array("trace"=>1));and print this after the result:
print "Request :\n".htmlspecialchars($client->__getLastRequest()) ."<p>"; print "Response:\n".htmlspecialchars($client->__getLastResponse())."<p>";you can display the raw SOAP XML.
6) Add a second method that prints "Have a nice day!", "Have a brave day!", etc.
In order to do this you need to change the server, the WSDL file and the
client.
In the server: add the second method into the existing class.
In the WSDL file: create two more messages (for input/output) and a second operation.
In the client: have the client call one message after the other. Note:
you can echo additional HTML code (eg <br>) in your client
file in order to format the display of the output.