Using XPath, SOAP, and More with Web Services

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).

Creating XML from a SimpleXML Object

The asXML() method returns a well-formed XML 1.0 string based on the SimpleXML object. Its prototype follows:

string simplexml_element->asXML()

An example follows:

$xml = simplexml_load_file("books.xml");
echo htmlspecialchars($xml->asXML());

This example returns the original XML document, except that the newline characters have been removed and the characters have been converted to their corresponding HTML entities.

Learning About a Node’s Children

Often you might be interested in only a particular node’s children. Using the children() method, retrieving them becomes a trivial affair. Its prototype follows:

object simplexml_element->children()

Suppose for example that the books.xml document is modified so that each book includes a cast of characters. The Hemingway book might look like the following:

     <title>The Sun Also Rises</title> 
     <author gender="male">Ernest Hemingway</author>
     <description>The masterpiece that launched Hemingway’s career.</description>
<character>Jake Barnes</character>
        <character>Lady Brett Ashley</character>
        <character>Robert Cohn</character>
        <character>Mike Campbell</character>

Using the children() method, you can easily retrieve the characters:

$xml = simplexml_load_file("books.xml");
foreach($xml->book[2]->cast->children() AS $character) {
echo "$character<br />";

This example returns the following:

Jake Barnes
Lady Brett Ashley
Robert Cohn
Mike Campbell

{mospagebreak title=Using XPath to Retrieve Node Information}

XPath is a W3C standard that offers an intuitive, path-based syntax for identifying XML nodes. For example, referring to the books.xml document, you could use the xpath() method to retrieve all author nodes using the expression /library/book/author :

array simplexml_element->xpath(string path)

XPath also offers a set of functions for selectively retrieving nodes based on value.

Suppose you want to retrieve all authors found in the books.xml document:

$xml = simplexml_load_file("books.xml");
$authors = $xml->xpath("/library/book/author");
foreach($authors AS $author) {
echo "$author<br />";

This example returns the following:

Jane Austen
Alberto Moravia
Ernest Hemingway

You can also use XPath functions to selectively retrieve a node and its children based on a particular value. For example, suppose you want to retrieve all book titles where the author is named Ernest Hemingway :

$xml = simplexml_load_file("books.xml");
$book = $xml->xpath("/library/book[author=’Ernest Hemingway’]");
echo $book[0]->title;

This example returns the following:

The Sun Also Rises

{mospagebreak title=SOAP} 

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 ( ):

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.

SOAP Messages

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 
            <return xsi:type="xsd:string">
            "My main objective is to be professional but to kill him.",
Mike Tyson (2002)

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.

{mospagebreak title=PHP’s SOAP Extension}

In response to the community clamor for Web Services–enabled applications, and the popularity of third-party SOAP extensions, a native SOAP extension was available as of PHP 5, and enabled by default as of PHP 6.0. This section introduces this object-oriented extension and shows you how to create both a SOAP client and server. Along the way you’ll learn more about many of the functions and methods available through this extension. Before you can follow along with the accompanying examples, you need to take care of a few prerequisites, which are discussed next.


PHP’s SOAP extension requires the GNOME XML library. You can download the latest stable libxml2 package from . Binaries are also available for the Windows platform. Version 2.5.4 or greater is required. If you’re running a version of PHP older than 6.0, you’ll also need to configure PHP with the –enable-soap extension. On Windows, you’ll need to add the following line to your php.ini file:


Instantiating the Client

The SoapClient() constructor instantiates a new instance of the SoapClient class. The prototype looks like this:

object SoapClient->SoapClient(mixed wsdl [, array options])

The wsdl parameter determines whether the class will be invoked in WSDL or non-WSDL mode; if in WSDL mode, set it to the WSDL file URI, otherwise set it to NULL . The options parameter is an array that accepts the following parameters. It’s optional for WSDL mode and requires that at least the location and url options are set when in non-WSDL mode:

actor : This parameter specifies the name, in URI format, of the role that a SOAP node must play in order to process the header.

compression : This parameter specifies whether data compression is enabled. Presently, Gzip and x-gzip are supported. According to the TODO document, support is planned for HTTP compression.

exceptions : This parameter turns on the exception-handling mechanism. It is enabled by default.

location : This parameter is used to specify the endpoint URL, when working in non-WSDL mode.

login : This parameter specifies the username if HTTP authentication is used to access the SOAP server.

password : This parameter specifies the password if HTTP authentication is used to access the SOAP server.

proxy_host : This parameter specifies the name of the proxy host when connecting through a proxy server.

proxy_login : This parameter specifies the proxy server username if one is required.

proxy_password : This parameter specifies the proxy server password if one is required.

proxy_port : This parameter specifies the proxy server port when connecting through a proxy server.

soap_version : This parameter specifies whether SOAP version 1.1 or 1.2 should be used. This defaults to version 1.1.

trace : This parameter specifies whether you’d like to examine SOAP request and response envelopes. If so, you’ll need to enable this by setting it to 1.

uri : This parameter specifies the SOAP service namespace when not working in WSDL mode.

Establishing a connection to a Web Service is trivial. The following example shows you how to use the SoapClient object to connect to a sports-related Web service I’ve created to retrieve a random boxing quote:

$ws =;
    $client = new SoapClient($ws);

However, just referencing the Web Service really doesn’t do you much good. You’ll want to learn more about the methods exposed by this Web Service. Of course, you can open up the WSDL document in the browser or a WSDL viewer by navigating to . However, you can also retrieve the methods programmatically using the __getFunctions() method, introduced next.

Please check back for the conclusion to this series.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye