Article
Mash Up Your Music with the Zend Framework
Yahoo News
For our demo application, we'll restrict our search to only relevant news stories, but this could be easily extended to include:
- searching for web pages
- searching for images
- searching local businesses and services
As with our previous web services, we load the web service class -- in this case Zend_Service_Yahoo -- then instantiate the class using our Yahoo application ID.
Zend_Loader::loadClass('Zend_Service_Yahoo');
$Yahoo = new Zend_Service_Yahoo('<- Your Yahoo! App ID ->');
Set the Options
There's a stack of options that you can use to refine your news search, although most of them have default values. For the entire list, visit the Yahoo Developer Network documentation.
In our mashup application, we're using the following options:
- return only three results
- display the first result first
- sort our news items by rank (i.e. relevance)
- display the results in English
- require results to contain all query terms listed
- show us results from MTV.com only
Here's how those options are represented in PHP code:
$newOptions = array(
'results' => 3,
'start' => 1,
'sort' => 'rank',
'language' => 'en',
'type' => 'all',
'site' => 'mtv.com'
);
To send a query to Yahoo with these options, we just call the newsSearch function, passing the options array and our query term:
$YahooResults = $Yahoo->newsSearch('The Rolling Stones', $newsOptions);
When our query is returned, we can check the results that we've received by inspecting a number of properties on the object that we get back:
totalResultsAvailable- the total number of results available for the searchResultSettotalResultsReturned- the total number of results returnedfirstResultPosition- the position of the returned news items relative to the complete result settotalResults()- the total number of items in the result set
Each of the news items returned has the following properties:
Title- title of the news itemUrl- URL of the news itemClickUrl- URL for linking to the news result itemSummary- news result summaryNewsSource- the company that distributed the articleNewsSourceUrl- URL of the article distributing companyLanguage- language in which the article is writtenPublishDate- date the article was published (UNIX timestamp)ModificationDate- date the article was last modified (UNIX timestamp)Thumbnail- thumbnail for the article, if one is available
In our demo application, we'll:
- check for a result from the search
- loop through the results
- display the title linked to the story
Here's the code:
$results = $Yahoo->newsSearch($Artist, $NewsOptions);
if($results->totalResultsReturned > 0){
foreach ($results as $result) {
$YahooNewsResults.= '<strong><a href="'.$result->ClickUrl
+ '">'.$HTMLFilter->filter($result->Title)
+ '</a></strong>'."\n";
$YahooNewsResults.= '<div class="newscredit">Posted by '
+ $result->NewsSource. ' on '.date('D jS M Y', $result->PublishDate)
+ '</div>'."\n";
$YahooNewsResults.= $HTMLFilter->filter($result->Summary)
+ '<br /><br />'."\n";
}
}
else {
$YahooNewsResults.= 'No News Items Found';
}
For more information on accessing Yahoo web services, refer to the Zend Framework documentation.
Bringing it All Together
In this article, we've explored several aspects of the Zend framework -- hopefully from what you've seen, you can appreciate just how powerful and simple a framework it is.
Along the way, we looked at some of the code that you would use to build a music mashup application -- don't forget to download all of the source code if you'd like to play with this application at home. The live demo is far from a polished, production-ready application, but it's a good starting point from which you can experiment with ideas of your own. The code is well commented and should give you a strong basis from which to create your own mashups, and experiment with the Zend Framework. Enjoy!