/* output:
J Bloggs
*/
Again, this is quite short and sweetin fact, most of the code is used to set the
entries in the $options array! We set the URI to match the server, and specify where
the location can be found. We also have the trace option enabled, which means we
can use some debugging functions. We instantiate the client, and then call the
functions in the ServiceFunctions class exactly as if it were a local class, despite
the SoapServer being on a remote server and the method call actually going via a
web request.
The debugging functions available to us are:
getLastRequest()
getLastRequestHeaders()
getLastResponse()
getLastResponseHeaders()
They show either the XML body or the headers of the request or response, and enable
us to check that were sending what we expected to send, as well as the format of
the response before it was parsed (this is very useful for those moments where debug
or unexpected output has been left in on the server side!).
Describing a SOAP Service with a WSDL
The example above used SOAP in a non-WSDL mode, but it is more common, and
perhaps simpler, to use a WSDL with SOAP services. WSDL stands for Web Service
Description Language, and its basically a machine-readable specification. A WSDL
describes at which URL a service is located, which methods are available, and what
parameters each method takes.
PHP cant generate WSDLs itself, and an accurate WSDL will also include informa-
tion about data types, which of course we lack in PHP. Most of the tools will take
into account any PHPDocumentor comments that you add regarding data types for
parameters, however, which does help. Some IDEs have built-in tools that can create
97APIs
a WSDL from a PHP class; alternatively, there is a WSDL generator available from
phpclasses.org.
7
Heres the WSDL for our example class:
chapter_03/wsdl.xml
<?xml version='1.0' encoding='UTF-8'?>
<definitions name="SimpleWSDL" targetNamespace="urn:SimpleWSDL"
xmlns:typens="urn:SimpleWSDL" xmlns:xsd="http://www.w3.org/2001/
XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="countWords"><part name="paragraph"
type="xsd:anyType"></part></message>
<message name="countWordsResponse"></message>
<message name="getDisplayName"><part name="first_name"
type="xsd:anyType"></part><part name="last_name"
type="xsd:anyType"></part></message>
<message name="getDisplayNameResponse"></message>
<portType name="ServiceFunctionsPortType">
<operation name="countWords"><input
message="typens:countWords"></input><output
message="typens:countWordsResponse"></output></operation>
<operation name="getDisplayName"><input
message="typens:getDisplayName"></input><output
message="typens:getDisplayNameResponse"></output></operation>
</portType>
<binding name="ServiceFunctionsBinding"
type="typens:ServiceFunctionsPortType"><soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
<operation name="countWords">
<soap:operation soapAction="urn:ServiceFunctionsAction">
</soap:operation>
<input><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
</soap:body></input>
<output><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
</soap:body></output>
</operation>
<operation name="getDisplayName">
<soap:operation soapAction="urn:ServiceFunctionsAction">
7
http://www.phpclasses.org/php2wsdl
PHP Master: Write Cutting-edge Code98
</soap:operation>
<input><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
</soap:body></input>
<output><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
</soap:body></output>
</operation>
</binding>
<service name="SimpleWSDLService">
<port name="ServiceFunctionsPort"
binding="typens:ServiceFunctionsBinding"><soap:address location=
"http://localhost/soap-
server.php"></soap:address></port>
</service>
</definitions>
As you can see, this is very definitely aimed at a target audience of machines, rather
than humans. Happily, the tools can generate the WSDL for us, and we can use this
to publish our service. In WSDL mode, we can create a client even more quickly:
ini_set('soap.wsdl_cache_enabled', 0);
$client = new SoapClient('http://localhost/wsdl');
Then we can go on and call the functions against SoapClient exactly as before.
With the WSDL, however, we have some additional functions. The SoapClient
object is aware of the functions available and which parameters can be passed; this
means that it can check we are sending sensible requests before we even send them.
Theres also a method, __getFunctions(), which can tell us which methods are
available on the remote service. Wed call that using this piece of code:
$functions = $client->__getFunctions();
var_dump($functions);
The SoapClient reads the WSDL, and gives us information about the functions in
this service in a format thats more useful to us than the raw WSDL XML.
99APIs

Get PHP Master now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.