Program Yahoo! with PHP 5
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.
The Code
Save the following code to your web server in a file called yahoo_search.php.
Tip
Don’t forget to grab a unique application ID for this script at http://developer.yahoo.net.
<?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>"; ...