The Zend Framework’s
Zend_Feed component makes publishing RSS and Atom feeds extremely simple.
The general workflow is this:
- 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.
- 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).
- 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
|
$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
|
$feed = array ();
$feed [ 'title' ] = "ZendCoding.com's Newest Stories" ;
$feed [ 'charset' ] = 'utf-8' ;
$feed [ 'language' ] = 'en-us' ;
$feed [ 'published' ] = time();
$feed [ 'entries' ] = array ();
foreach ( $stories ->fetchAll( $select ) as $story ){
$entry = array ();
$entry [ 'title' ] = $story ->title;
$entry [ 'link' ] = $story ->url;
$entry [ 'description' ] = $story ->teaser;
$entry [ 'content' ] = $story ->description;
$entry [ 'lastUpdate' ] = $story ->modified;
$entry [ 'comments' ] = $story ->commentsUrl;
$entry [ 'commentsRss' ] = $story ->commentsRssUrl;
$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' );
|
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.
|
$feedString = $feedObj ->saveXML();
$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