Generating your own Dynamic RSS Feed using PHP

Date Published: 26/02/2009 14:18

RSS feeds can now be found in all corners of the world wide web and have become a important part of the typical web users diet. Allowing your users to quickly access certain parts of your site check for new content introduces many benefits which any informative website cannot live without. Maintaining RSS feeds can be quite tiresome as many feeds are maintained manually by the user. Adding entries manually not only takes time but also increases the possibility of not updating it at all leaving it stagnant and useless. I have created a small PHP static class which can be easily integrated into any web site to easily generate your own RSS feed.

The Static Class

To use this class just insert it prior to where it will be used. I would recommend including this in any custom site library you may be using already.

<?php
class RSSFeed {
   function getFeed($site_title, $site_description, $copyright_details, $feed_items, $domain=false){
      if (!$domain) $domain = $_SERVER["SERVER_NAME"];
      $feed = "<?xml version="1.0" encoding="UTF-8"?>
";
      $feed .= "<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
";
      $feed .= "<title>$site_title</title>
<description>$site_description</description>
<link>http://$domain</link>
<copyright>$copyright_details</copyright>
";
      foreach ($feed_items as $item) {
         $item["EpochPublishDate"] = date("D, d M Y H:i:s O", $item["EpochPublishDate"]);
         $feed .= "<item>
<guid>http://$domain" . $item["ItemAbsoluteURL"] . "</guid>
<title>" . $item["ItemTitle"] . "</title>
<description>" . $item["ItemDescription"] . "</description>
<link>http://" . $_SERVER["SERVER_NAME"] . $item["ItemAbsoluteURL"] . "</link>
<pubDate>" . $item["EpochPublishDate"] . "</pubDate>
</item>
";
      }
      $feed .= "</channel>
</rss>";
      return $feed;
   }
}
?>

Setting the Content-Type Header

You must first add the following line before any output occurs to signal to the users browser that the file is indeed an RSS feed (although most modern browsers will work without this). The header also informs the browser that the feed is encoded in UTF-8.

header("Content-Type: application/rss+xml; charset=UTF-8");

Usage

To generate your RSS feed you must first create an instance of the class above and provide it with various details about your website.

echo RSSFeed::getFeed($site_title, $site_description, $copyright_details, $items);

The $site_title parameter is the title of your website and will be included in the head of the RSS feed. You must also provide a description via $site_description and information regarding copyright on your site through $copyright_details. These things can be set statically but in order to generate the content you will need to create a multi-dimensional associative array and pass it through the $items variable. You can write your own code to produce this array which will best link to your specific back end. The array however must meet the following specifications. Each item associative array within the overall items array should be structured like the following.

  1. ItemTitle - Title of feed item.
  2. ItemDescription - Description of feed item.
  3. ItemAbsoluteURL - Absolute URL of item in relation to the root of the site.
  4. EpochPublishDate - Epoch date of when the item was published.

array("ItemTitle" => "Title Here", "ItemDescription" => "Description Here", "ItemAbsoluteURL" => "/url/to/item/here.php", "EpochPublishDate" => Epoch Time Here);

Simply create an array like the one above for each item in your RSS feed ordered by date (newest first) and put them all in one big array of arrays. Once passed through the static class will then output this information as individual items in your RSS feed and calculate the date of the item from the epoch date provided. Echo out the output of the function and your new dynamic RSS feed is complete. All that is left to do is to link your pages to your new feed so that your users will be notified by their browser that your site has an RSS feed.

Hooking up Your New RSS Feed

Applying an RSS feed to your page is as easy as adding one line of HTML to your pages. If possible you should add this line to all pages which your RSS feed relates so that the user can access it from anywhere within your site.

<link rel="alternate" type="application/rss+xml" title="RSS Feed Title Goes Here" href="/path/to/your/rss/feed.php" />

Just copy the line above and change the title and href attributes to fit your site and paste it between the head tags of the pages where you want to see it. If you then visit a page where this tag is implemented you should be able to access the feed by clicking your browsers RSS link (in FireFox and Safari this is in the address bar and in Internet Explorer 7 it is on the toolbar).

Conclusion

The logos of PHP and RSS

There we have it, a dynamic RSS feed which will always be up to date (as long as your $items array is dynamically generated ofcourse). I will shortly write a similar article relating to using the same process to produce an atom feed for your site. If you have an issues using any of the code above or want assistance when dynamically creating your $items array feel free to comment below and I will try my best to help.

Comments

Sorry comments are currently disabled for maintenence

5 Most Recent Articles

Manually Triggering Events in ASP.NET from JavaScript

A quick guide for ASP.NET developers on how to manually trigger ASP.NET events from JavaScript.

Advanced Use of MySQL Stored Procedures

An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.

Using MySQL Stored Procedures and Extending MySQLi in PHP

A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.

Reading and Writing to Excel Spreadsheets in Python

An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.

Interact with the Web Using Python and the HTTP Library

This is an introduction to making HTTP requests from a python script/application using httplib.

Sponsors