Take advantage of some of the latest
features in PHP to quickly add Yahoo! data to PHP-powered pages.
The recursively named PHP Hypertext Processor language is a
popular choice for building dynamic web applications. In fact, Yahoo! itself has
made PHP its development platform of choice across the company.
The PHP platform
is continually evolving, and the latest version Version 5 includes a handy XML
parser called SimpleXML. As the name implies,
it's easy to work with. And as long as the XML that SimpleXML is parsing is
fairly simple, it's the perfect tool for getting XML data into objects PHP can
easily manipulate.
Yahoo! Search Web Services responses
definitely qualify as simple XML, and this hack shows how easy it is to request
and parse this data with PHP. You'll need PHP 5 for
this hack, but you won't need any external modules.
<?php // yahoo_search.php // Accepts a search term and shows the top results. // Usage: yahoo_search.php?p=<Query> // // You can create an AppID, and read the full documentation // for Yahoo! Web Services at http://developer.yahoo.net/ // Set your unique Yahoo! Application ID $appID = "insert your app ID"; // Grab the incoming search query, and encode for a URL $query = $_GET['p']; $query = urlencode($query); if ($query == "") { print "usage: yahoo_search.php?p=<Query>"; die; } // Construct a Yahoo! Search Query with only required options $language = "en"; $req_url = "http://api.search.yahoo.com/"; $req_url .= "WebSearchService/V1/webSearch?"; $req_url .= "appid=$appID"; $req_url .= "&query=$query"; $req_url .= "&language=$language"; // Make the request $yahoo_response = file_get_contents($req_url); // Parse the XML $xml = simplexml_load_string($yahoo_response); // Initialize results counter $i = 0; ?> <html> <body> <h2>Yahoo! Search Results</h2> <ol> <?php // Loop through the items returned, printing them out foreach ($xml->Result as $result) { $i++; $title = $result->Title; $summary = $result->Summary; $summary = preg_replace("/</i","<",$summary); $clickurl = $result->ClickUrl; $url = $result->Url; print "<li><div style=\"margin-bottom:15px;\">"; print "<a href=\"$clickurl\">$title</a><br />"; print "$summary<br />"; print "<cite>$url</cite></div></li>\n"; } ?> </ol> -- Results Powered by Yahoo! </body> </html>
This script uses the value of the querystring variable
p to build a Yahoo! Web Search request URL and fetches the XML with the
file_get_contents() function. Once the script has the XML in the
$yahoo_response string, it calls the SimpleXML function simplexml_load_string( ),
which parses the XML and makes the data available to PHP as an object. Finally,
the script loops through the objects, using print to send the data to
the browser.
To run the script, point your web browser to the location of
the script on your server and add the querystring variable p:
http://example.com/yahoo_search.php?p=insert word
You can add multiple words by encoding spaces for URLs. For
example, here's the search string for "PHP encoding":
http://example.com/yahoo_search.php?p=PHP%20encoding
As the results indicate, you can read the official documentation for PHP's SimpleXML function at http://www.php.net/simplexml. With this function, working with Yahoo! Search Web Services data is much more intuitive than with earlier versions of PHP.