Get Google News as JSON by Parsing rss feed

Standard

Google-News-Nuhil
As you know already that, Google News search API has been deprecated few years ago. Still developers need to have Google news as an usable format like JSON for different purpose & development platforms.

Google still provides the news service but in rss format. So, we can have those data for using just by parsing and reformatting them. I wrote a very small php class which will first search  news on Google by specific keywords and load the XML. Then it will parse the XML carefully to dissect parts of news & finally will provide a formatted JSON data set.

As of JSON, all knows that how universal this data format is to implement on several software development platforms. Hope this tiny class help some one 🙂

The class file:

<?php
 **
 * Google-News feed parser and JSON provider Class
 *
 * @package None
 * @author Nuhil Mehdy <nuhil@nuhil.net>
 */
class GoogleNews {

    public $searchQuery = 'Good News';

    public function __construct ($searchQuery) {
            if(!empty($searchQuery)) {
                    $this->searchQuery = $searchQuery;
            }

            $this->numberOfNews = 5;
    }

    public function __toString() {
            return $this->getNews();
    }

    public function setSearchQuery($searchQuery) {
            if(!empty($searchQuery)) {
                    $this->searchQuery = $searchQuery;
            }
    }

    public function setNumberOfNews ($numberOfNews) {
            if(!empty($numberOfNews)) {
                    $this->numberOfNews = (int) $numberOfNews;
            }
    }

    public function getNews () {
            return $this->processNews();
    }

    private function processNews() {
            $loadXml = simplexml_load_file(urlencode('http://news.google.com/news?q='.$this->searchQuery.'&num='.$this->numberOfNews.'&output=rss'));

            $news = array();

            $i = 0;
            foreach ($loadXml->channel->item as $item)
            {
                preg_match('@src="([^"]+)"@', $item->description, $match);
                $newsSections = explode('</pre>
', $item->description);

 $news[$i]['title'] = (string) $item->title;
 $news[$i]['image'] = $match[1];
 $news[$i]['link'] = (string) $item->link;
 $news[$i]['news_source'] = strip_tags($newsSections[1]);
 $news[$i]['short_story'] = strip_tags($newsSections[2]);

 $i++;
 }

 $result = array('news' => $news, 'status' => '200', 'message' => 'OK');

 return json_encode($result);
 }

}
?>

Example usage:

<?php
include('news.php'); 
$news = new GoogleNews(); 
$news->setSearchQuery('Bangladesh');
$news->setNumberOfNews('10');
echo $news->getNews();
?>

You will find this source code on github also. Feel free to use and or fork.

Advertisements

3 thoughts on “Get Google News as JSON by Parsing rss feed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s