HomePHP Page 3 - Using XPath, SOAP, and More with Web Services
SOAP - PHP
In this fourth part of a five-part series on Web Services, you'll learn about SimpleXML objects, SOAP, and more. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
The Postal Service is amazingly effective at transferring a package from party A to party B, but its only concern is ensuring the safe and timely transmission. The Postal Service is oblivious to the nature of the transaction, provided that it is in accordance with the Postal Service’s terms of service. As a result, a letter written in English might be sent to a fisherman in China, and that letter will indeed arrive without issue, but the recipient would probably not understand a word of it. The same holds true if the fisherman were to send a letter to you written in his native language; chances are you wouldn’t even know where to begin.
This isn’t unlike what might occur if two applications attempt to talk to each other across a network. Although they could employ messaging protocols such as HTTP and SMTP in much the same way that we make use of the Postal Service, it’s quite unlikely one protocol will be able to say anything of discernible interest to the other. However, if the parties agree to send data using the same messaging language, and both are capable of understanding messages sent to them, the dilemma is resolved. Granted, both parties might go about their own way of interpreting that language (more about that in a bit), but nonetheless the commonality is all that’s needed to ensure comprehension. Web Services often employ the use of something called SOAP as that common language. Here’s the formalized definition of SOAP, as stated within the SOAP 1.2 specification (http://www.w3.org/TR/SOAP12-part1/):
SOAP is a lightweight protocol intended for exchanging structured information in a decentralized, distributed environment. It uses XML technologies to define an extensible messaging framework providing a message construct that can be exchanged over a variety of underlying protocols. The framework has been designed to be independent of any particular programming model and other implementation-specific semantics.
Keep in mind that SOAP is only responsible for defining the construct used for the exchange of messages; it does not define the protocol used to transport that message, nor does it describe the features or purpose of the Web Service used to send or receive that message. This means that you could conceivably use SOAP over any protocol, and in fact could route a SOAP message over numerous protocols during the course of transmission. A sample SOAP message is offered in Listing 20-5 (formatted for readability).
Listing 20-5. A Sample SOAP Message
<?xml version="1.0" encoding="ISO-8859-1" ?> <SOAP-ENV:Envelope SOAP ENV:encodingStyle="http:// schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/ xmlns:xsd="http:// www.w3.org/2001/XMLSchema" xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC=http://schemas.xmlsoap.org/soap/encoding/ xmlns:si="http:// soapinterop.org/xsd"> <SOAP-ENV:Body> <getRandQuoteResponse> <return xsi:type="xsd:string"> "My main objective is to be professional but to kill him.", Mike Tyson (2002) </return> </getRandQuoteResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
If you’re new to SOAP, it would certainly behoove you to take some time to become familiar with the protocol. A simple Web search will turn up a considerable amount of information pertinent to this pillar of Web Services. Regardless, you should be able to follow along with the ensuing discussion quite easily because the PHP SOAP extension does a fantastic job of taking care of most of the dirty work pertinent to the assembly, parsing, submission, and retrieval of SOAP messages.