Wednesday, October 12, 2011

How to Publish RSS and Atom Feeds Using the Zend Framework

The Zend Framework’s Zend_Feed component makes publishing RSS and Atom feeds extremely simple.
The general workflow is this:
  1. Collect the items you wish to publish. These can come from your database, a static file, or any other source. They are collected into an array with a specific keys corresponding to the different parts of your feed.
  2. Import your feed into Zend_Feed. This allows you to manipulate your feed using Zend, which will automagically create a well formed document of the type specified (rss or atom).
  3. Send the feed. With a call to the send() method, your feed is output and you’re done!
Zend_Feed handles all of the looping and formatting for you, so you don’t have to worry about creating a well formed document that adheres to the standards of the RSS or Atom protocols. You are still responsible for getting your data into a usable array, but most of the hard work is done for you.

Collect the Items

To get started, we need to collect our items. These are generally your newest stories or objects, and in most cases will come from a database. The object here is to loop through the items you want to publish, formatting them in a way that Zend_Feed will understand. To see the entire schema with all options, please visit Importing Feeds in the Zend Framework documentation.
This example reads items from a database. If you have a different data source, such as a static file, the process is the same, just alter your code appropriately.
Connect to a database using Zend_Db









//setup our db connection
$db = Zend_Db::factory('PDO_PGSQL', array(
    'host' => '192.168.1.107',
    'port' => 6543,
    'username' => 'web_user',
    'password' => '7jh##219)9#',
    'dbname' => 'lyo_main'
    )
);
Setup our table and $select object



$stories = new Stories(array('db' => $db));
 
$select = $stories->select()->order('created')->limit(15);
Now loop through all the most recent stories, formatting them for use in Zend_Feed





































//Create an array for our feed
$feed = array();
 
//Setup some info about our feed
$feed['title']          = "ZendCoding.com's Newest Stories";
 
$feed['link']           = 'http://www.zendcoding.com/newest-stories.rss';
 
$feed['charset']    = 'utf-8';
 
$feed['language']   = 'en-us';
 
$feed['published']  = time();
 
$feed['entries']    = array();//Holds the actual items
 
//Loop through the stories, adding them to the entries array
foreach($stories->fetchAll($select) as $story){
    $entry = array(); //Container for the entry before we add it on
 
    $entry['title']     = $story->title; //The title that will be displayed for the entry
 
    $entry['link']      = $story->url; //The url of the entry
 
    $entry['description']   = $story->teaser; //Short description of the entry
 
    $entry['content']   = $story->description; //Long description of the entry
 
    //Some optional entries, usually the more info you can provide, the better
    $entry['lastUpdate']    = $story->modified; //Unix timestamp of the last modified date
 
    $entry['comments']  = $story->commentsUrl; //Url to the comments page of the entry
 
    $entry['commentsRss']   = $story->commentsRssUrl; //Url of the comments pages rss feed
 
    $feed['entries'][]  = $entry;
}

Import Into Zend_Feed

Now we have our feed contained in the $feed variable. The next step is to import the array into Zend_Feed so it can create the feeds. This is done with Zend_Feed::importArray(). Additionally, you can specify the type of feed to generate (rss or atom), by passing a second argument:

$feedObj = Zend_Feed::importArray($feed, 'rss'); //Or importArray($feed, 'atom');

Output

The last step is outputting our feed. Depending on your application, you may want to return the feed as a string because you want to save it or you’re just not ready to output it yet, or you may wish to send the appropriate headers and the feed all at once.





//Return the feed as a string, we're not ready to output yet
$feedString = $feedObj->saveXML();
 
//Or we can output the whole thing, headers and all, with
$feedObj->send();
Congratulations, you now have an RSS or Atom feed on your site! And thanks to the Zend Framework, we don’t have to worry about correctly formatting XML documents in the RSS or Atom protocols, we just call send() and voila! we have a feed.

No comments:

Post a Comment