Displaying an RSS Feed

One of the "secrets" of successful web sites is that they keep users coming back. So what can you do that will attract users and keep them coming back? You attract them by providing information that is useful to the user and you keep them coming back by continually updating that information so that it's fresh each time the user visits.

Creating a web site is the easy part. Keeping it fresh is an ongoing challenge that can be a lot of work. Fortunately, there is a tool that can make that job a lot easier. That tool is called RSS. RSS is an acronym meaning Really Simple Syndication. RSS is a small subset of XML.

“Creating a web site is the easy part. Keeping it fresh is an ongoing challenge that can be a lot of work.”

There are a lot of free "RSS feeds" that allow you to get a multitude of different things. You can display that RSS data in a specialized application called a news aggregator, or (as we want) you can display it in a web page. While, as I say, there are a lot of free feeds, they are not totally benevolent exercises. Normally, an RSS feed will provide a short description of an item and provide a link to the complete item on the provider's web site. In other words, you can display a short description of a news story, for example, but the complete story will be displayed on the provider's site, which you can link to.

This is a simple example of using RSS to get a news feed from wired.com. To begin, the URL to get the raw XML file for the RSS feed is http://www.wired.com/news/feeds/rss2/0,2610,,00.xml. If you click that link, you'll see the XML that is served by the RSS feed. Note that it's just a raw XML file, so you'll have to use your browser's Back button to get back to this page.

So, how do we turn that XML into a presentable page that you can display on your web site? It's not as hard as you might think! PHP has built-in functions that will parse an XML file. I've used those functions to build a generic wrapper that will accept a URL to an RSS feed and parse it into a PHP object. You can download that file and use it to display RSS information on your site. The usage is hopefully easy.

UPDATE: The rssFeed class has been updated 3 May, 2005. The original version used the PHP function file_get_contents() to get the content of the remote XML file. Recently, however, my web host disabled the allow_url_fopen setting in PHP. That prevented the file_get_contents() function from getting the content of any remote files.

At first, I was a bit puzzled and perhaps a little angry about that. Then I saw what some people were doing with those functions. Some people were actually grabbing code from remote sites and executing it, without the slightest idea what was in the files! This was creating a huge problem for the host. Note that the previous version of the rssFeed class did not execute foreign code. It simply parsed the XML data. There was never a security problem with it. The problem was in the way some people were using some of the functions the class relied upon.

After understanding the problem better, I agreed that this was a good move on their part. My host suggested using the PHP cURL library instead. I tried that, but found it to be less reliable. Never wanting to admit defeat, I re-wrote the class to use direct socket I/O. It now works reliably.

If you have an older version, I'd suggest you download the updated version. It has no external dependencies. You don't need the allow_url_fopen setting to be turned on and you don't need the cURL library. Usage is exactly the same. You will not need to change any existing code that uses the class. Simply replace the old rssreader.php file with the new one.

Example Code

   // Include the file that does all the work

   // This is the URL to the actual RSS feed. Change this value
   // if you want to show a different feed.

   // Create an instance of the rssFeed object, passing it
   // the URL of the feed
   $rss=new rssFeed($url);

   // If there was an error getting the data
      // Show the error
      print "<h1>Error:</h1>\n<p><strong>$rss->error</strong></p>";

      // Otherwise, we have the data, so we call the parse method

      // The showHeading can accept a paramater that will be used
      // as the tag to wrap the heading. In this case, we're wrapping
      // the title in an <h1> tag

      // Display the image if there is one

      // If the RSS feed provides a link
         // Display it
         print "<p>Provided courtesy of:<br>\n";

      // Display the description

      // Show the news stories

So, what does that do? Well, here's an example using the exact code above, with minimal attempt at styling. What could be easier?

Styling the Output

You have your choice of tags with which to wrap the title displayed by the showHeading method. Simply pass the tag you want to use (without the angle brackets) to the showHeading call. For example, to wrap the heading in <h2> tags, call the method as


You can use whatever CSS you want to style the tag used to display the heading. News stories are displayed in a definition list. The story headline is displayed as a defined term, a <dt>, and the description is displayed as the definition, a <dd>. You can use CSS to style those two tags like you want them to appear.

If any of the styles conflict with other styles used in your page, I would suggest simply wrapping the RSS data in a container, a <div> for example, and style that. Let's say we wanted to make the links green in our RSS display:

#rss a{color: green;}

Then, just put the RSS output inside a <div id="rss">:

<div id="rss">
   $rss=new rssFeed($url);

Where to Find RSS Feeds

So, now you can display an RSS feed. Where do you find interesting feeds to display? Here's a short list to get you started:


I hope this is helpful to you. It should get you started on the road to providing continually fresh information on your web site. Good luck!