Display Cached RSS Feed In Your Website

Martin Brinkmann
May 6, 2009
Updated • Aug 9, 2018
Development
|
3

You may remember the announcement of our new web project Appnews which displays software updates in a friendly easily accessible way on its website and as an RSS fee (Note: Appnews has been discontinued since).

What I wanted to do now is to display the five newest items of the RSS feed in the sidebar of this blog to both promote the appnews service but also provide a new service to my readers as they would be able to see the software updates immediately in the sidebar.

What I needed was a high traffic friendly way of displaying the RSS feed and that meant caching.

Without caching the script would request the content of the RSS feed on every page hit and the server would need to parse it to display its content which would slow down the web server tremendously and cause lots of extra traffic.

One of the better scripts to display cached RSS feeds in a website is SimplePie. It might look a bit intimidating at first glance as it provides lots of options to customize the display.

It is however very easy to display a feed with just a few lines of code. Start by downloading SimplePie from the website. Extract it to the computer system and upload it to the root folder of the website. You should also create a directory called cache in that root folder and make it writeable (chmod to 777).

Depending on your server configuration, you may be able to change the rather "open" permissions to more strict ones to make attacks more difficult.

Now add the following two lines to the top of the php file where you want to display the feed in. The first line defines the location of the simplepie.inc file on the web server (which is in the simplepie directory in root), the second the feed that you want to display on the website.

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/simplepie/simplepie.inc';
$feed = new SimplePie('http://feedproxy.google.com/Ghacksnet');
?>

Now place the following code in the location on the website where the feed should be displayed. This will fetch the five newest feed items and display them in the selected location.

<ul>
<?php foreach ($feed->get_items(0, 5) as $item): ?>
<li>
<a href="<?php print $item->get_permalink(); ?>">
<?php print $item->get_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>

And that's it. SimplePie offers a wealth of additional functions and settings to display more than one feed or mix feeds. The default caching time is 60 minutes. You can read the documentation on the SimplePie website which offers installation and use examples, and provides details about the API the service maintains.

Summary
Article Name
Display Cached RSS Feed In Your Website
Description
How to display the most recent entries of a RSS feed on a website and cache those entries to limit look ups.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Previous Post: «
Next Post: «

Comments

  1. phrench said on June 18, 2009 at 2:16 am
    Reply

    Thanks! Way better than all those crappy rss widgets!

  2. Sameer said on May 8, 2009 at 2:48 pm
    Reply

    thanks i was looking out for something like this

  3. Rarst said on May 6, 2009 at 10:04 pm
    Reply

    SimplePIe rules. :) Appnews site itself is built on it.

    However easier way on WordPress platform might be just using native RSS widget. It also has caching and based on MagpieRSS (that would be changed to SimplePie in WP 2.8).

    At my blog I mostly used hardcoded snippets but my theme became such code mess internally that I widgetized most of it later.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.