XML Parsing with PHP by John M. Stokes
Author:John M. Stokes [John M. Stokes]
Language: eng
Format: epub
Tags: Core Programming
Publisher: php[architect]
Published: 2015-04-29T16:00:00+00:00
SOAP and WSDL
SOAP originally meant “Simple Object Access Protocol.” But it is no longer an acronym and is just a terminology used for this technolog.7. It is a technique of defining functions that can be called remotely over a network like the Internet, that is, remote procedure calls via a standard protocol, usually HTTP8. In recent years, SOAP has fallen in popularity in favor of REST due to the higher processing overhead and the complexity of defining SOAP messages. SOAP messages, both request and response, are encoded in XML.
To further our use of SOAP, we can publish details about our SOAP functions and data structures via Web Services Description Language, or WSDL. WSDL is another XML-based language9.
Let’s define, via SOAP, and publish, via WSDL, a web service for getting a title from an ISBN for our library.xml document.
First, our WSDL interface shown in Listing 9.5.
Listing 9.5: WSDL interface
<?xml version="1.0" encoding="UTF-8"?> <definitions name="getTitleService" targetNamespace="http://www.libraryexample.com/booklist" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ex="http://www.libraryexample.com/booklist" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <message name="getTitleRequest"> <part name="ISBN" type="xs:string"/> </message> <message name="getTitleResponse"> <part name="Title" type="xs:string"/> </message> <portType name="TitlePortType"> <operation name="getTitle"> <input message="ex:getTitleRequest"/> <output message="ex:getTitleResponse"/> </operation> </portType> <binding name="TitleBinding" type="ex:TitlePortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="getTitle"> <soap:operation soapAction="getTitle"/> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded"/> </input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded"/> </output> </operation> </binding> <service name="getTitleService"> <documentation> WSDL File for getTitleService </documentation> <port binding="ex:TitleBinding" name="TitlePort"> <soap:address location="http://localhost:8080/soap/servlet/rpcrouter"/> </port> </service> </definitions>
A WSDL interface usually sits on a server waiting for users to call it, so there’s little need to generate it dynamically with PHP. The WSDL format can be complicated and difficult to write, so there are a number of generators available on the internet.
We can create a SOAP request for this WSDL using SimpleXML as in Listing 9.6.
Listing 9.6: Creating a SOAP request with SimpleXML
$SOAPstring = '<?xml version="1.0"?> <soap:envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> </soap:envelope>'; $SOAP = new SimpleXMLElement($SOAPstring); $body = $SOAP->addChild('soap:body'); $getTitle = $body->addChild( 'l:getTitle', null, 'http://www.libraryexample.com/booklist' ); $ISBN = $getTitle->addChild('l:ISBN', '0765328321'); echo $SOAP->asXML();
The tags that appear within the <soap:body> tag are application-specific tags, in our case <l:getTitle> and <l:ISBN>. As you can see, we defined our own namespace for our application with the “l” (lower case “L”, as in “Library”) for those application-specific tags.
The SOAP response might look something like this:
$SOAPresponse = '<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding" > <soap:Body> <l:getTitleResponse xmlns:l="http://www.libraryexample.com/booklist"> <l:Title>Halo: The Fall of Reach</l:Title> </l:getTitleResponse> </soap:Body> </soap:Envelope>';
And we can parse out the title with a simple XPath query.
$SOAP = new SimpleXMLElement($SOAPresponse); $SOAP->registerXPathNamespace( 'l', 'http://www.libraryexample.com/booklist' ); $titleArray = $SOAP->xpath('//l:Title'); $title = $titleArray[0]; echo (string)$title;
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
