Display Cached RSS Feed In Your Website - gHacks Tech News

Display Cached RSS Feed In Your Website

You might remember the announcement of our new web project Appnews which basically displays software updates in a friendly easily accessible way. 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 contents on every page hit which would slow down the web server tremendously.

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).

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.

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




  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. 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.

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

      thanks i was looking out for something like this

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

      Thanks! Way better than all those crappy rss widgets!

    Leave a Reply