Article

Mash Up Your Music with the Zend Framework

Page: 1 2 3

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 search
  • ResultSettotalResultsReturned - the total number of results returned
  • firstResultPosition - the position of the returned news items relative to the complete result set
  • totalResults() - the total number of items in the result set

Each of the news items returned has the following properties:

  • Title - title of the news item
  • Url - URL of the news item
  • ClickUrl - URL for linking to the news result item
  • Summary - news result summary
  • NewsSource - the company that distributed the article
  • NewsSourceUrl - URL of the article distributing company
  • Language - language in which the article is written
  • PublishDate - 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:

  1. check for a result from the search

  2. loop through the results

  3. 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!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: