Sihan’s Blog

Tech Talks

Simple RSS reader using PHP

with 14 comments

RSS is a basic XML format for publishing syndicated content. The root element of the document is rss, which contains a single channel element. The channel element contains metadata about the feed, including its title, language, and URL. It also contains various stories enclosed in item elements. Each item has a link element containing a URL and either a title or a description (usually both) that contain plain text.
Here is an example feed from google news:


<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<generator>NFE/1.0</generator>
<title>Google News - Sci/Tech</title>
<link>http://news.google.com/?ned=us&amp;topic=t</link>
<language>en</language>
<webMaster>news-feedback@google.com</webMaster>
<copyright>&amp;copy;2008 Google</copyright>
<pubDate>Wed, 06 Aug 2008 07:13:30 GMT</pubDate>
<lastBuildDate>Wed, 06 Aug 2008 07:13:30 GMT</lastBuildDate>
<image>
<title>Google News</title>
<url>http://news.google.com/intl/en_us/images/news_res.gif</url>
<link>http://news.google.com/</link>
</image>
<item>
<title>Delta to Offer Wi-Fi on US Flights - New York Times</title>
<link>http://news.google.com/news/url?sa=T&amp;ct=us/0-0&amp;fd=R&amp;url=http://www.nytimes.com/2008/08/06/technology/06delta.html%3Fref%3Dbusiness&amp;cid=1233786045&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNHnUSPm9sCPP4k2gWPBDboZAiYtCw
</link>
<guid isPermaLink="false">tag:news.google.com,2005:cluster=498a14bd
</guid>
<pubDate>Wed, 06 Aug 2008 01:10:42 GMT</pubDate>
<description>&lt;table border=0 width= valign=top cellpadding=2 cellspacing=7&gt;&lt;tr&gt;&lt;td width=80 align=center valign=top&gt;&lt;font style=&quot;font-size:85%;font-family:arial,sans-serif&quot;&gt;&lt;a href=&quot;http://news.google.com/news/url?sa=T&amp;ct=us/0i-0&amp;fd=R&amp;url=http://www.geekzone.co.nz/content.asp%3Fcontentid%3D7864&amp;cid=1233786045&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNF4sPd1YWYzXRyLVYtIYVl_ndmW4g&quot;&gt; .......
</description>

</item>
<item>
<title>LiMo lands seven new phones - Register</title>
<link>http://news.google.com/news/url?sa=T&amp;ct=us/19-0&amp;fd=R&amp;url=http://www.theregister.co.uk/2008/08/05/limo_lands_seven_new_phones/&amp;cid=1233302260&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNEILOMvAvv5fwcFWpYLWWJVs7vc5Q</link&gt;
<guid isPermaLink="false">tag:news.google.com,2005:cluster=4982b2f4
</guid>
<pubDate>Tue, 05 Aug 2008 01:00:06 GMT</pubDate>
<description>&lt;table border=0 width= valign=top cellpadding=2 cellspacing=7&gt;&lt;tr&gt;&lt;td width=80 align=center valign=top&gt;&lt;font style=&quot;font-size:85%;limo-platform/&amp;cid=1233302260&amp;ei=Gk-ZSLbVI4PGlgS_94jVBA&amp;usg=AFQjCNHS1CWmT1jipl0N-Q-VEOnxM-TmRg&quot;&gt;&lt;img src=http://news.google.com/news?imgefp=hOtr_7v_dyoJ&amp;....</description>
</item>
</channel>
</rss>

Using PHP’s SimpleXML(introduced in PHP5) api it is very easy to access different nodes of the xml above. Like, if we want to get the title of the rss at google news we can write,

$url = "http://news.google.com/?ned=us&topic=t&output=rss"; // url of google news
$xml = simplexml_load_file($url); //loading the document
$title = $xml->channel->title; //gets the title of the document.

The beauty of the function "simplexml_load_file()" is : it does not only load and parse the document but also points to the root element of the document (which is 'rss' here).

Using ‘Simple XML api’ the following code snippet prints news from google news rss :

<?php
$url = "http://news.google.com/?ned=us&topic=t&output=rss";
$rss = simplexml_load_file($url);
if($rss)
{
echo '<h1>'.$rss->channel->title.'</h1>';
echo '<li>'.$rss->channel->pubDate.'</li>';
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$published_on = $item->pubDate;
$description = $item->description;
echo '<h3><a href="'.$link.'">'.$title.'</a></h3>';
echo '<span>('.$published_on.')</span>';
echo '<p>'.$description.'</p>';
}
}
?>

About these ads

Written by sihan

August 6, 2008 at 12:36 PM

14 Responses

Subscribe to comments with RSS.

  1. I’m not able to get this to work. Do you have an example of where you have it working?

    kp

    September 5, 2008 at 5:01 PM

  2. Hi,
    I have tested it on my local server and its working nice. I can’t give you a live url right now. But I can help you work with your problem.
    Please let me know what kind of problems you are facing, perhaps you can give me a trace of the error you are getting with it.

    Also Please note that:
    1. Simple Xml support must be enabled in your server to work with this code. You can know it by “phpinfo()” function.Simple xml is available in php5 or later.
    2. Please replace (ā€) with double quotes and (ā€˜) with single qoute, as wordpress replaces them in code.

    sihan

    September 5, 2008 at 6:42 PM

  3. Thanks for your quick response! I just replaced the quotes & got it to work… didn’t realize that WordPress messes with them.

    Thanks again!

    kp

    September 5, 2008 at 6:57 PM

  4. Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

    Alexwebmaster

    March 3, 2009 at 9:48 AM

  5. Thank you very much for this example!
    Now, I use this on all my site’s.

    Shaun

    August 8, 2009 at 4:25 PM

  6. […] on this one from sihan: $url = "http://news.google.com/?ned=us&topic=t&output=rss&quot;; $rss = […]

  7. Iā€™m not able to get this to work.

    Simple XML support is enabled on my server. I’m using blogspot, then I don’t need to change (” “) by (‘ ‘).

    What can I do?

    With this code I always get ‘Problems loading RSS feed’

    <?php

    $url = 'http://news.google.com/?ned=us&topic=t&output=rss&#039;;
    $rss = simplexml_load_file($url);
    if($rss)
    {
    echo '’.$rss->channel->title.”;
    echo ”.$rss->channel->pubDate.”;
    $items = $rss->channel->item;
    foreach($items as $item)
    {
    $title = $item->title;
    $link = $item->link;
    $published_on = $item->pubDate;
    $description = $item->description;
    echo ‘‘.$title.’‘;
    echo ‘(‘.$published_on.’)';
    echo ”.$description.”;
    }
    }
    else
    {
    echo ‘ Problems loading RSS feed';
    }
    ?>

    Oscar Sandoval Torres

    March 22, 2010 at 9:03 PM

  8. Nice Posting, this posting useful for me :D

    casmadi

    July 28, 2010 at 10:08 AM

  9. hey.

    thanks for the script, it’s working like a charm!
    Good work.

    Greetings,
    Wouter
    The Netherlands

    woutie

    October 1, 2010 at 4:15 PM

  10. Hi,

    Thanks for the script. It works like a charm.

    Please can you tel me how to control the number of RSS feeds to be displayed?

    Meher

    December 8, 2010 at 4:44 PM

    • Hi Meher,
      You are welcome.
      If you don’t want to display all the feeds then you can use ‘for’ loop instead of ‘foreach’. i.e something like this:

      $num = 2;//no. of feeds you want to display
      
      if($num>count($items)){
         $num = count($items);	
      }
      for($i=0;$i< $num;$i++){
      	$link = $items[$i]->link;
      	$published_on = $items[$i]->pubDate;
      	$description = $items[$i]->description;
              ......
              ......
              ......
      }
      

      Thanks!

      sihan

      December 9, 2010 at 12:48 PM

  11. Hi,

    Thank you for reply.

    I tried the for loop. But it did not success. No RSS Feeds are displayed.

    File Name : rss_reader2.php

    Instead following PHP Errors (Notices)are displayed on the page:
    1) Notice: Undefined variable: item in rss_reader2.php on line 18
    2) Notice: Trying to get property of non-object in rss_reader2.php on line 18
    3) Notice: Undefined variable: item in rss_reader2.php on line 19
    4) Notice: Trying to get property of non-object in rss_reader2.php on line 19
    5) Notice: Undefined variable: item in rss_reader2.php on line 20

    Code Spec given Below:
    <?php
    $url = "http://news.google.com/?ned=us&topic=t&output=rss&quot;;

    $rss = simplexml_load_file($url);
    if($rss)
    {
    echo '’.$rss->channel->title.”;
    echo ”.$rss->channel->pubDate.”;
    $items = $rss->channel->item;

    $num = 2;//no. of feeds you want to display

    if($num>count($items))
    {
    $num = count($items);
    }
    for($i=0;$ititle;
    $link = $item->link;
    $published_on = $item->pubDate;
    $description = $item->description;
    echo ‘‘.$title.’‘;
    echo ‘(‘.$published_on.’)';
    echo ”.$description.”;
    }
    }
    ?>

    Please can you guide me as to where am I going wrong?

    Meher

    December 9, 2010 at 3:25 PM

  12. Hi,

    thanks for this, you save me a lot of time.

    James

    James

    October 27, 2011 at 4:23 PM

  13. Even if we’re in 2011, this is still pretty usefull : cheers !

    pneu pas cher

    December 15, 2011 at 10:27 PM


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: