AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 489


$query = $_GET["query"];
$url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=
XXX-GET-YOUR-OWN-KEY-XXX&safe_search=1&per_page=
10&content_type=1&text=$query";
$result = file_get_contents($url);
/* Check response status */
list($version,$status,$msg) = explode(' ',$http_response_header[0], 3);

if ($status != 200)
echo "Your call to the web service returned an error status of $status.";
else
{
/* take XML string and make DOM tree */
$domtree = new DOMDocument();
$domtree->loadXML($result);
/* convert from XML to JSON */
$transform = new XML2JSON();
$result = $transform->convertToJSON($domtree);
print $result;
}
?>

FIGURE 10-1 Searching for pictures via a Web Service
Free download pdf