Building Your News Service API - Adding Fresh Content and Interactivity to Your Web Site
Posted by: Jeffrey Olchovy
In the previous installment of Taming the Wild Wild Web, we managed to get a hold of the necessary components so that we can begin to assemble our Yahoo! News Service API Mashup. When fully complete, we will have a page division that will auto-populate the latest news stories’ descriptions, URLs, and titles into our Web site. As stated in Part II of this series, this will ultimately be helpful in generating fresh content for our Web site on a daily basis. One caveat that I will mention again is that Yahoo! limits the requests made from a single IP address each day - so proper attention must be paid before implementing such an application at the production level.
Let’s continue where we left off:
We had just assembled our default REST resquest, passing only the two required parameters (’appid’ and ‘query’) thus far. We’ll take a look at that to jog our memories:
“http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=paul%20mccartney”
(if you remember, we were using Yahoo!’s Demo API key as the ‘appid’ and ‘Paul McCartney’ as our URL encoded query)
Now let’s decide what other parameters we’d like to pass to grab a specialized result set for our application.
Since we want the most recent news stories populating our list, we’re going to add the ’sort’ argument with the value set to ‘date’ to aptly sort the results by the most recent news item. Then we’ll pass both the ‘language’ and ‘results’ parameters so that we can filter our results to English only and to five stories maximum (values: ‘en’ and ‘5′ respectively). Lastly, we’ll choose to have the result set return data to us in serialized PHP format by passing the ‘output’ argument with the value of ‘php’.
Our query now should look something like this:
“http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query=paul%20mccartney&sort=date&language=en&output=php&results=5”
Now in order to make the REST request, we’ll use PHP’s file_get_contents function and assign it to the variable ‘data.’ Because this will be returned as serialized PHP, we must take measures to get ourselves some workable code. We’ll use the unserialize fuction to do so and assign that to the variable ‘results’. This will give us a multidimensional array where the values are set and assigned based upon Yahoo!’s Web Service API. Check out this schema document for the proper structure of our resulting set.
Once we determine if there is any resulting data, it is as simple as traversing the array from the field ResultSet which houses each Result. Within each individual result therein lies the data we’re after - the title, the summary, the clickUrl (Yahoo!’s proprietary URL of the news story for tracking purposes), the News Source, and the News Source URL.
By outlining the data in such a way, it seems like the most feasible method of extracting it would be by iterating through the array ['ResultSet']['Result'] with a simple foreach loop, echoing our desired data in its proper markup shells.
We’ll also take the care of providing a friendly message if there were no results to be found.
This entire script weighs in at mere 22 lines with ample white space!
Check out the image below for the full code (complete with inline styles for simple viewing pleasure).
(Please strip out the styles if you are to use this as a foundation for your next API mashup! It will be extremely messy if you do not!)
Jeffrey Olchovy is a Web developer, designer and marketing strategist.




You must be logged-in to post a comment. Log-in/Register