Parse Yahoo! Web Service REST Calls with PHP

This HOWTO assumes that you already know how to make a Yahoo! Web Service request and are wondering how to parse the XML, JSON, or Serialized PHP returned by your request. This page contains these sections:

Parsing JSON

In addition to XML, some Yahoo! Web Services can also return JavaScript Object Notation (JSON). JSON is a compact data notation that has some advantages over XML. Not all Yahoo! Web Services are capable of returning JSON. For details on JSON, consult our JSON documentation.

PHP developers have two choices for parsing JSON:

  • PHP-JSON - a PHP extension written in C.
  • JSON-PHP - a library written in PHP.

PHP-JSON, since it is written in C, is much faster than JSON-PHP, but it requires that you have administrative access to your PHP installation so that you can install it. PHP-JSON is freely available here.

JSON-PHP is a proposed PEAR library. It is freely available here and can be used by simply downloading the library and including it in your working directory.

Once acquired and, in the case of PHP-JSON, installed, their use is straightforward.

First, your Yahoo! Web Services request must include the output=json parameter so that it returns JSON:

$request =  'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Madonna&results=4&output=json'; 

For PHP-JSON, you simply invoke the json_decode function:

$phpobj1 = json_decode($response);

For PHP-JSON, you include the library, create a Services_JSON object, and then invoke the decode method:

require_once('JSON.php'); 
$json = new Services_JSON(); 
$phpobj2 = $json->decode($response);

A complete code sample is available here.

Serialized PHP

In addition to XML, some Yahoo! Web Services can also return serialized PHP. Serialized PHP is a convenient data notation that is easier for PHP scripts to consume than XML. For details on Serialized PHP, including parsing, consult our Serialized PHP documentation.

Parsing XML With PHP 4

There are a lot of ad hoc ways to parse XML in PHP 4. Using the DOM XML extension is one way we recommend. DOM XML is a PHP extension that is bundled with PHP 4 distributions but is not installed by default. To determine if it's installed, you can run the phpinfo function and browse the output:

<?php
echo phpinfo();
?>
 

Using the DOM XML Extension

The DOM XML extension is a document object model (DOM) parser that parses your XML into a convenient hierarchal structure. Once parsed, developers can access the various data elements by traversing the tree structure. For details about DOM XML features, consult the DOM XML documentation.

To parse your XML, you use the domxml_open_mem function on the XML returned from your Yahoo! Web Services request:

$dom = domxml_open_mem($response, DOMXML_LOAD_PARSING, $errors);

The variable $dom now points to a tree-structured object containing the data. The $errors variable will contain any parsing errors.

You must traverse the structure to get at the data. The method of traversal will vary somewhat depending on the structure of your XML. For our various Yahoo! Search and Maps Web Services, which return the number of results as attributes in the root tag, and use the <Result> tag to hold each item returned, this routine will suffice:

function xml_to_result($dom) {
   $root = $dom->document_element();
   $res['totalResultsAvailable'] = $root->get_attribute('totalResultsAvailable');
   $res['totalResultsReturned'] = $root->get_attribute('totalResultsReturned');
   $res['firstResultPosition'] = $root->get_attribute('firstResultPosition');
   $node = $root->first_child();
   $i = 0;
   while($node) {
      switch($node->tagname) {
      case 'Result':
         $subnode = $node->first_child();
         while($subnode) {
            $subnodes = $subnode->child_nodes();
               if(!empty($subnodes)) {
                     foreach($subnodes as $k=>$n) {
                     if(empty($n->tagname)) {
                           $res[$i][$subnode->tagname] = trim($n->get_content());
                     } else {
                           $res[$i][$subnode->tagname][$n->tagname]=trim($n->get_content());
                     } 
                  }
               }
            $subnode = $subnode->next_sibling();
         }
         break;
      default:
         $res[$node->tagname] = trim($node->get_content());
         $i--;
         break;
      } 
      $i++;
      $node = $node->next_sibling();
   } 
   return $res;
}

For other Yahoo! Web Services, such as the Flickr Web Services, the xml_to_result traversal routine will have to be altered to accurately traverse the DOM hierarchy.

A complete code sample is available here.

Parsing XML With PHP 5

PHP 5's SimpleXML extension is the preferred method for XML parsing. PHP 5 also has an improved DOM parsing extension that is not compatible with PHP 4's DOM XML extension. Neither of these extensions are installed by default. To determine if they are installed, you can run the phpinfo function and browse the output:

<?php
echo phpinfo();
?>

Using the DOM Extension

The DOM extension is a document object model (DOM) parser that parses your XML into a convenient hierarchal structure. Once parsed, developers can access the various data elements by traversing the tree structure. For details about DOM features, consult the DOM documentation.

To parse your XML, create a new DOM object and then use the loadXML function. Specify the XML 1.0 specification and UTF-8 encoding as arguments for your DOM object:

$dom = new DOMDocument('1.0', 'UTF-8'); 
if ($dom->loadXML($response) === false) { 
   die('Parsing failed'); 
}

The variable $dom now points to a tree-structured object containing the data. You must traverse the structure to get at the data. The method of traversal will vary somewhat depending on the structure of your XML. For our various Yahoo! Search and Maps Web Services, which return the number of results as attributes in the root tag, and use the <Result> tag to hold each item returned, this routine will suffice:

function xml_to_result($dom) {
   $root = $dom->firstChild;
   foreach($root->attributes as $attr) $res[$attr->name] = $attr->value;
   $node = $root->firstChild;
   $i = 0;
   while($node) {
      switch($node->nodeName) {
         case 'Result':
         $subnode = $node->firstChild;
         while($subnode) {
            $subnodes = $subnode->childNodes;
            foreach($subnodes as $n) {
               if($n->hasChildNodes()) {
                  foreach($n->childNodes as $cn) $res[$i][$subnode->nodeName][$n->nodeName]=trim($cn->nodeValue);
               } else $res[$i][$subnode->nodeName]=trim($n->nodeValue);
            }
            $subnode = $subnode->nextSibling;
         }
         break;
         default:
         $res[$node->nodeName] = trim($node->nodeValue);
         $i--;
         break;
      }
      $i++;
      $node = $node->nextSibling;
   }
   return $res;
}

For other Yahoo! Web Services, such as the Flickr Web Services, the xml_to_result traversal routine will have to be altered to accurately traverse the DOM hierarchy.

A complete code sample is available here.

Using the SimpleXML Extension

PHP 5's SimpleXML extension makes quick work of parsing XML. Unlike the DOM and DOM XML extensions, SimpleXML parses your XML into a PHP object structure that can be easily accessed. For details about SimpleXML features, consult the SimpleXML documentation.

To parse your XML, use the simplexml_load_string function:

$phpobject = simplexml_load_string($response); 

Once parsed, the elements are available in a SimpleXML object. XML attributes (if any) can be collected into an array for easier access:

foreach($phpobject->attributes() as $name=>$attr) { 
   $res[$name]=$attr; 
}

You can look at the object data structure by using the PHP print_r or var_dump functions:

print_r($phpobject);
var_dump($phpobject);

If you are running PHP 5.1 or newer, you can catch specific SimpleXML parsing errors using the libxml_get_errors function.

A complete code sample is available here.

For More Information

For more information on using PHP with Yahoo! Web Services APIs, see The Yahoo! Developer Network PHP Developer Center.