Generating your own Dynamic Atom Feed using PHP

Date Published: 26/02/2009 17:27

I recently wrote an article containing a small PHP static class which could be used to generate a dynamic RSS feed for your site. Since atom feeds are increasing in popularity I felt it would be a good idea to also provide a small guide on producing an atom feed to go alongside your new dynamically generated RSS feed. You may have noticed that here at Dev Explorer I offer both RSS and atom feeds to my users, I do this to ensure that my site is compatible with as many people and platforms as possible, something we should all consider throughly when creating web content.

The Static Class

I have once again produced a static class which can be accessed in a similar manner to the RSS class I wrote earlier. You may even want to combine the two into one Feed class with separate methods for RSS and atom.

class AtomFeed {
   function getFeed($site_title, $contact_email, $feed_location, $feed_items, $domain=false){
      if (!$domain) $domain = $_SERVER["SERVER_NAME"];
      $feed = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
      $feed .= "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n";
      $feed .= "<title>$site_title</title>\n<link rel=\"self\" href=\"$feed_location\"/>\n<updated>" . date('Y-m-d\TH:i:s', $feed_items[0]["EpochPublishDate"]) . "</updated>\n";
      $feed .= "<author>\n<name>$site_title</name>\n<email>$contact_email</email>\n<uri>http://$domain</uri>\n</author>\n";
      foreach ($feed_items as $item) {
         $item["EpochPublishDate"] = date("D, d M Y H:i:s O", $item["EpochPublishDate"]);
         $feed .= "<entry>\n<title>" . $item["ItemTitle"] . "</title>\n<link href=\"http://$domain" . $item["ItemAbsoluteURL"] . "\"/>\n<id>http://$domain" . $item["ItemAbsoluteURL"] . "</id>\n<updated>" . $item["EpochPublishDate"] . "</updated>\n<summary type=\"xhtml\">" . $item["ItemDescription"] . "</summary>\n</entry>\n";
      }
      $feed .= "</feed>";
      return $feed;
   }
}

Setting the Content-Type Header

In order for every users browser/news reader to properly understand your atom feed you must first transmit what data type you are going to send, coming from a PHP file make the client believe they are receiving a text/HTML document. Place the following line in your PHP file before any output begins.

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

Usage

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

echo AtomFeed::getFeed($site_title, $site_description, $feed_url, $items);

Atom feeds differ slightly from RSS feeds in that they do not take a site description or copyright details. The $site_title parameter is the title of your website and will be included in the head of the atom feed. You must also provide a description via $site_description and url of the atom feed on your site through $feed_url. 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 atom feed ordered by date (newest first) and put them all in one big array of arrays. The items passed to this static class are identical to those passed to my RSS static class. This means you can write a small function which generates this array personal to your own site and then point it to which ever type of feed you need at that time. Working like this will save you time and reduce the amount of code you need to write. Once passed through the static class will then output this information as individual items in your atom feed and calculate the date of the item from the epoch date provided. Echo out the output of the function and your new dynamic atom 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 atom feed.

Hooking up Your New Atom Feed

Applying an Atom 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 Atom feed relates so that the user can access it from anywhere within your site.

<link rel="alternate" type="application/atom+xml" title="Atom Feed Title" href="/path/to/atom/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 feed link.

Conclusion

The logos of PHP and RSS

Now if you have implemented both the dynamic RSS and atom feeds a whole range of different users using different software on different platforms can access your content.

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