Simple Google News Parser in PHP

Requirements: HTML (basic understanding), PHP (basic understanding)

Ever needed to put an automatically updating Google News feed for a specific topic on your website? This simple PHP script parses the Google News RSS feed and displays it in a customizable layout.

Want to see how it works? Click here for a demo.

 

The Code

Create a standard PHP page, and put this in your stylesheet or in your head tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<style type="text/css">
.newsarticle {
    font-size:14px;
    margin-bottom: 30px;
    width: 500px;
}

.newsarticle div {
    font-size:14px;
}

.newsarticle h5 {
    margin:0;
    font-weight: 400;
    color:#888888;
}

a.rmorelink, a.rmorelink:visited {
    font-size:12px;
    color: #555555;
    text-decoration:none;
    float:right;
    text-transform:uppercase;
}

a.rmorelink:hover {
    color: #000000;    
}

</style>

Next, add the following wherever you want your Google news articles to show up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

$chars = 250; /* Characters in the news story */

$sterm = $_GET['q']; /* Search term */

/* alternatively, $sterm = "your+term+here"; */

$news = simplexml_load_file('https://news.google.com/news/feeds?hl=en&gl=ca&q='.$sterm.'&um=1&ie=UTF-8&output=rss'); /* URL of Google News RSS feed*/

$feeds = array();

$i = 0;

foreach ($news->channel->item as $item) 
{
    if ($i < 5) {
        preg_match('@src="([^"]+)"@', $item->description, $match);
        $parts = explode('<font size="-1">', $item->description);

        $feeds[$i]['title'] = mb_convert_encoding((string) $item->title, "HTML-ENTITIES", "UTF-8");
        $feeds[$i]['link'] = (string) $item->link;
        $feeds[$i]['story'] = mb_convert_encoding(strip_tags($parts[2]), "HTML-ENTITIES", "UTF-8");
        
        $stringCut = substr($feeds[$i]['story'], 0, $chars);
        $feeds[$i]['story'] = substr($stringCut, 0, strrpos($stringCut, ' ')).' ...<br /><a class="rmorelink" href="'.$feeds[$i]['link'].'">Read More »</a>';         
        
        $feeds[$i]['date'] = (string) $item->pubDate;
    
        echo '<div class="newsarticle"><a href="';
        print($feeds[$i]['link']);
        echo '" taget="_blank"><b>';
        print($feeds[$i]['title']);
        echo '</b></a><br /><h5>';
        print($feeds[$i]['date']);
        echo '</h5><div>';
        print($feeds[$i]['story']);
        echo '</div></div>';

        $i++;
    }
}

?>

Great! That’s all! The script will automatically use the q variable (e.g. yoursite.com/index.php?q=your+term) to parse Google News. You can also set it to search for only a specific term. In that case, replace

1
$sterm = $_GET['q'];

with

1
$sterm = "your+term+here"

Lastly, if you want the user to be able to search custom topics, add the following form to the page.

1
2
3
4
5
6
<b>Enter your search query here:</b>

<form action="index.php" method="GET">
  <input type="text" name="q" /> 
  <input type="submit" /> 
</form>

That’s it! It’s super easy and super customizable! Enjoy!

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Digg
  • Email
  • RSS

April 20, 2013 at 2:05 am | Posted in: Tutorials, Web Design | No comments

Leave a Reply

Your email address will not be published. Required fields are marked *