This post originated from an RSS feed registered with PHP Buzz
by Stephan Schmidt.
Original Post: PHP5-Client for Yahoo's Webservice API
Feed Title: a programmer's best friend
Feed URL: http://blog.php-tools.net/rss.php?version=1.0
Feed Description: The blog of PHP Application Tools
Our new member Daniel Wiegand (wiegi) implemented a PEAR compatible client for Yahoo's new REST-based webservice API. From the Yahoo developer site: "Yahoo! Search Web Services allow you to access Yahoo content and services in your favorite programming languages. This means you can now build Yahoo directly into your own applications." By using Daniel's PHP5 client you do not need to worry about the protocol or the resulting XML format, everything is done in plain PHP: <?PHP $yahoo = new Services_Yahoo($appId); try { // do a simple search $result = $yahoo->searchWeb('PEAR');
printf('<a href="%s">%s</a><br />', $entry['ClickUrl'], $entry['Title']); printf('<i>%s</i><br /><br />', $entry['Summary']); } } catch (Services_Yahoo_Exception $e){ echo $e; } ?>
Besides searchWeb() the client provides more methods to use the different web services offered by Yahoo:
searchImages()
searchVideos()
searchNews
searchLocal()
Each of the methods can be used with several parameters that need to be passed in a fixed parameter order or you may pass a associative array that contains any parameters you want to pass.
As Daniel has been using PHP5 all of these methods return a Services_Yahoo_Result object that provides methods to return the total number of search results but can be iterated like a normal object, thanks to SPL. This result object offers a lot more, as you can use it to fetch the next page using the same search parameters: <?PHP $yahoo = new Services_Yahoo($appId); try { // do a simple search $result = $yahoo->searchWeb('Graceland');
We would have loved to propose this to PEAR, but there already is a draft for a Services_Yahoo package by Martin Jansen, but until now he hasn't showed any of his code. Hopefully Daniel and Martin could be working on a package together.