Home » Programming » How to create your own RSS or XML feed reader with SimplePie

How to create your own RSS or XML feed reader with SimplePie



Would you like to create your own RSS or XML reader — just like Google Reader (although not that many features) — that you can publish under your own website? You can do it with PHP and SimplePie. SimplePie is a PHP class that you can easily use to format and publish RSS and XML feeds.

Why would you create your own RSS or XML reader?

  • To publish fresh content on your website or blog automatically (without plagiarizing)
  • To create your own RSS/XML repository that you can fully control
  • Maybe, to create a website like AllTops.com

Whatever may be the reason, it can be great fun getting XML and RSS feed links from other websites and then seeing them appearing as feeds under your own website.

We assume you already have Apache and PHP running on your machine. Read how to install Apache, PHP and MySQL in Ubuntu. On Windows, you can simply download XAMPP and install it. Once this is sorted out, you need to download simplepie.inc and store it in the folder where you are going to create your RSS/XML reader with PHP.

For this example let’s use the RSS link from our favorite Mashable, http://feeds.mashable.com/Mashable. Our small script will extract the latest feeds from this RSS link and publish them in whichever manner we want to. Here’s the code to first connect to simplepie.inc:

<?php
require_once “simplepie.inc”;
?>

This loads the class simplepie.inc and makes all its great features available to you. Now we begin to process the Mashable feed.

<?php
$mfeed = “http://feeds.mashable.com/Mashable”;
$feedinstance = new SimplePie();
$feedinstance->set_feed_url($mfeed);
$feedinstance->init();
$feedinstance->handle_content_type();
?>
<h1><a href=”<?php echo $feedinstance->get_permalink(); ?>”><?php echo $feedinstance->get_title(); ?></a></h1>
<p><?php echo $feedinstance->get_description(); ?></p>

We aren’t done yet; let’s briefly see what’s happening here. Most of the lines are self-explanatory. handle_content_type() ensures that the content is sent to the browser as text/html and the UTF-8 character set. The last two lines simply publish the link to the main website header and the description. And now the remaining code:

<?php
foreach($feedinstance->get_items() as $item)
{
?>
        <p><a href=”<?php echo $item->get_permalink(); ?>”><?php echo $item->get_title(); ?></a><br />
        <?php echo $item->get_description(); ?><br />
        Posted on: <?php echo $item->get_date(); ?></p>
<?php
}
?>

This simply loops through the RSS feed and publishes the feed information one by one. Using CSS and other web page elements you can format the output according to your own preference.

If you want to maintain multiple feeds you can simply store them in a text file or in a MySQL database. Read how to connect to a MySQL database with PHP. Suppose you have a text file named myfeeds.txt where you store every feed in a new line, something like,

feed1
feed2
feed3
feed4

You can read the file, retrieve the RSS or XML feeds one by one and publish them using the code mentioned below:

<?php
$fp = fopen(”myfeeds.txt”, “r”(;
// open in read-only mode
$feedinstance = new SimplePie();
while(!feof($fp))
{
        $mfeed = fgets($fp, 4056);
        $feedinstance->set_feed_url($mfeed);
        $feedinstance->init();
        $feedinstance->handle_content_type();
        ?>
        <h1><a href=”<?php echo $feedinstance->get_permalink(); ?>”><?php echo $feedinstance->get_title(); ?></a></h1>
        <p><?php echo $feedinstance->get_description(); ?></p>
        <?php
        foreach($feedinstance->get_items() as $item)
        {
        ?>
                <p><a href=”<?php echo $item->get_permalink(); ?>”><?php echo $item->get_title(); ?></a><br />
                <?php echo $item->get_description(); ?><br />
                Posted on: <?php echo $item->get_date(); ?></p>
        <?php
        }
}
fclose($fp);
?>