Parsing the Response Data


PHP has both Document Object Model (DOM) and Simple API for XML (SAX) parsers for XML. The PHP DOM extension parses your XML into a convenient hierarchal structure, which allows access to various data elements by traversing the tree structure. The PHP SAX parser reads XML data sequentially, requires less memory, and is faster, but does not allow for random access to XML document elements.

Here is a quick comparison between SAX and DOM XML parsing:

Table 1. 

Better for large document handling because less memory is required Requires more memory
Better for retrieving a specific value from a document Allows random access, but is slower when trying to find a specific value
Read-only access, no modification of document possible Allows modification of document
Cannot be used with XPath and XSLT, which need access to the entire tree Allows for the use of XPath and XSLT
Some XML can be validated All XML can be validated

This tutorial will be using the PHP DOM extension to parse the XML response from the Yahoo! Image Search Web service. There are several PHP DOM extensions available to parse XML. We'll be looking at the simplest to use, which is aptly called SimpleXML for PHP5. Because the PHP development team has stated that support for PHP4 will not be offered after August 8, 2008, we recommend using or migrating to PHP5. We have provided this domxml code sample to demonstrate how to parse XML with PHP4.

Getting Started

PHP5's SimpleXML extension makes parsing XML easy by creating an easily accessed PHP object structure from a string. Our example will only be showing a small range of what can be done with SimpleXML. To see more details, consult the SimpleXML documentation.

  1. In Making a REST Request, we received a response from the Yahoo! Image Search Web service that included the HTTP header as well as the response data in the form of XML. We need to extract the image data that is embedded in the XML for our Web page, but before doing that we need to first remove the HTTP header information. The PHP function strstr is used here to return the XML data in the response that follows the HTTP header.
  2. Let's take a look now at the XML returned from the response with just one result for simplicity. If you were to display the request returned from curl_exec earlier, the output should look like an unformatted version of the XML below with five results instead of one. On examining the XML, we see that the root is <ResultSet> and its child is the <Result node>. Our goal is to parse the XML to extract the title, summary, and URL from the <Result> node for our Web page.
  3. The function simplexml_load_string from the SimpleXML extension takes the XML response as a string argument and returns a SimpleXML object that can be easily traversed. The foreach loop returns the child Result from which the values for the nodes Title, Url, and Summary can be extracted. We save these values to a variable and output the results as a table. With PHP, we can do all of this with only ten lines of code:
  4. Let's run our code now from a Web browser. Your program should display a Web page similar to the one below.

Congratulations, you have finished making the Water Bear Gallery Web application using the Yahoo! Image Search Web Service. Now that you know how to make a REST request and parse XML, making a simple Web application that uses any Yahoo! Web service should be easy. The next step is to create a more efficient and robust application in Caching for Better Reliability and Performance.

Exploring (Optional)

The DOMDocument extension is excellent for creating XML, but you can also parse XML and is better than SimpleXML for parsing complex XML documents or XML documents of mixed content. This code sample will introduce the syntax and ways to implement the DOMDocument to parse XML. Other PHP extensions for parsing XML include XMLReader, which is suitable for large XML documents, and the PHP SAX parser.

Table of Contents