It’s been a while since I’ve planned on developing a RSS writer that fulfills most my needs by supporting the various feed formats. Although the necessity was the prime force behind it, a discussion with Hasin and Emran has put the actual fire in. We were discussing about what can be added next in the Orchid - “PHP framework for the rest of us” and suddenly it hit me. At last, it’s finally complete and I’ve named it "PHP Universal Feed Generator", as it generates both ATOM and RSS feeds.

Supported versions:

  • RSS 1.0 (which officially obsoleted RSS 0.90)
  • RSS 2.0 (which officially obsoleted RSS 0.91, 0.92, 0.93 and 0.94)
  • ATOM 1.0

Download: 

Features:

  • Generates RSS 1.0, RSS 2.0 and ATOM 1.0 feeds
  • All feeds are are validated by feed validator.
  • Supports all possible feed elements.
  • Simple and easy to define channel and feed items
  • Implements appropriate namespaces for different versions.
  • Automatically converts date formats.
  • Generates UUID for ATOM feeds.
  • Enables usage of subtags and attributes. (example: image and encloser tags)
  • Completely Object oriented in PHP5 class structure.
  • Handles CDATA encoding for required tags.
  • Nearly same code for generating all kinds of feed

A minimum example

It’s a minimum example of using this class. I am generating a RSS 2.0 feed from retrieved data from a MySQL database. There are more examples in the download package for different versions.

<?php
  // This is a minimum example of using the Universal Feed Generator Class
  include("FeedWriter.php");
  
  //Creating an instance of FeedWriter class. 
  $TestFeed = new FeedWriter(RSS2);
  
  //Setting the channel elements
  //Use wrapper functions for common channel elements
  $TestFeed->setTitle(‘Testing & Checking the RSS writer class’);
  $TestFeed->setLink(‘http://www.ajaxray.com/projects/rss’);
  $TestFeed->setDescription(‘This is test of creating a RSS 2.0 feed Universal Feed Writer’);
  
  //Image title and link must match with the ‘title’ and ‘link’ channel elements for valid RSS 2.0
  $TestFeed->setImage(‘Testing the RSS writer class’,‘http://www.ajaxray.com/projects/rss’,‘http://www.rightbrainsolution.com/images/logo.gif’);
  
    //Retriving informations from database
    mysql_connect("server", "mysql_user", "mysql_password");
    mysql_select_db("my_database");

    $result = mysql_query("Your query here");

 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        //Create an empty FeedItem
        $newItem = $TestFeed->createNewItem();
        
        //Add elements to the feed item    
        $newItem->setTitle($row[‘title’]);
        $newItem->setLink($row[‘link’]);
        $newItem->setDate($row[‘create_date’]);
        $newItem->setDescription($row[‘description’]);
        
        //Now add the feed item
        $TestFeed->addItem($newItem);
    }
  
  //OK. Everything is done. Now genarate the feed.
  $TestFeed->genarateFeed();
?>

Shhhh….a universal feed reader is on the way ;)