Wednesday 7 May 2008

Show RSS feed on your website

PHPThere are 2 ways of showing on your website news coming from other sites RSS feeds in PHP.

• The first is to open the feed like any other file and use PHP functions on strings to build your own parser.

The disadvantage is that this method is not "portable": depending on the RSS feed version (1, 2, Atom) there are diffrent syntaxes and the code working for a feed, won't work for another.


• The second is to use an existing library, where these problem were already solved by others.. :-)

We can find 2 PEAR (a working group close to PHP authors) packages, which allow feed reading. This is the easier solution when we only need the title and the link. Unfortunately they are to simple if you need to collect more data like the publication date of the articles.

Another libray often used is MagpieRSS. If the insstallation is very easy, the feed is parsed in a table and we will have the same kind of problems: sometimes the date will be in the "pubDate" key of the table, sometimes in the "updated" key... Difficult to adapt to several feeds with the same code. :-(


Fortunately, there is a solution: SimplePie. :-) With it, some ways exist to collect the title, the link, the content, the date and even emages with the same function on any RSS feed version.

This is an object-oriented library, a bit harder at first sight, it is however quite as simple as the others.

Basic example:

require_once('../simplepie.inc');
$feed = new SimplePie('http://simplepie.org/blog/feed/');

It's even possible to load several feeds, and print them in a single output! You only need to give a table containing all the feed URL to the function set_feed_url.


require_once('../simplepie.inc');
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://simplepie.org/blog/feed/',
'http://azure-dev.kizone.net/feed'
));

The feed is now loaded, we have then to create a loop to read the results.

foreach ($feed->get_items() as $article)

In each foreach loop cycle we collect the title with $article->get_title(), the link with
$article->get_permalink(), etc...

The complete example is here in the SimplePie documentation.

This is not difficult: each data to collect have its own function. We can easily collect the date or the article categories with the same function from any feed version.

After having tested various solutions, I definitively stay on SimplePie. :-)


café Did this article help you? 
Buy me a coffee!

4 answers at “Show RSS feed on your website”

  1. 1
    Anthony Damasco (blog.anthonydamasco.com) said:

    This article was very helpful, thanks

  2. 2
    Yuvaraj (crapcorner.com) said:

    I want to know, how to parse the RSS feed into my html webpage. What code should I give for that?

  3. 3
    David (azure-dev.kizone.net) said:

    If your page is only HTML without PHP or another language, you can't. Of course your server need to execute some commands.

  4. 4
    Aneeq (phphelp.co) said:

    There are several ways to read RSS feed in PHP, but this one is surely one of the easiest.

    <?php

    $feed = file_get_contents('http://www.mywebsite.com/rss/');
    $rss = new SimpleXmlElement($feed);

    foreach($rss->channel->item as $entry)
    {
      echo "<p><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></p>";
    }

    ?>

    Source:
    http://phphelp.co/2012/04/23/how-to-read-rss-feed-in-php/
    OR
    http://addr.pk/a0401

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev