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.
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.
<?php foreach ($feed->get_items(0, 5) as $item): ?>
<a href="<?php print $item->get_permalink(); ?>">
<?php print $item->get_title(); ?></a>
<?php endforeach; ?>
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.Advertisement
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.