RSS Feed in Zend Framework
- William Estep
- Programming
- May 26, 2008
Going to get a little geeky here. But I wanted to share a simple, and effective way to create a simple rss feed of content in the Zend Framework .
The new ClubReading site is separated into two parts, the blog, and the books. The books site is now using Disqus for the comments, and so far Disqus is working out great, but that’s a topic for another day. The blog is a wordpress blog. What I wanted to do was display a couple or three of the recently added books from the books site on the sidebar of the ClubReading Blog. My thought was to create a simple rss feed of the most recent books, and use the standard wordpress rss widget to display the feed content in the sidebar. But - how to create the rss feed? Turns out it was pretty simple thanks to a great article by Alex Netkachov called “Syndicate content with Zend Framework Zend_Feed classes” .
What is an RSS feed?
Simply put, RSS, Really Simple Syndication, is a file format. Think of it as a form letter. Like a business letter, because the format is standardized, the reader knows where to find the from address, to address, salutation, content…and so on. An RSS feed does the same thing by taking content and making it available in a standardized format.
What does an RSS feed look like?
RSS feeds are in xml format. XML, or the Extensible Markup Language, presents any type of information within descriptive tags. Tags are closed in <> and can be anything. Don’t confuse the xml tags with html. For example, in html to bold a bit of text, just enclose the text in the <b>
tag, for example,<b>Bold</b> is in bold
would show in the browser as Bold is in bold. XML tags work the same way, they open and close. So we might have a description tag like this:
<description>Description text</description>
There are several types or standards of RSS feeds. A great place to start is the RSS 2.0 Specification article at Harvard Law. As the article states,
RSS is a dialect of XML. All RSS files must conform to the XML 1.0 specification, as published on the World Wide Web Consortium (W3C) website.
RSS feeds basically consist of a header section that describes the feed, and a repeating section for items or content. So, the first thing I did was create a sample RSS file to work with as a template. I want to feed to show the Book Title, Author and who entered the book. Here’s the sample I came up with:
ClubReading Books>
http://books.clubreading.com/index/rss
An online community for the avid book lover.>
Mon, 26 May 2008 21:28:50 +0000
Zend Framework Zend_Feed
en-us
http://blogs.law.harvard.edu/tech/rss
Clans of the Alphane Moon>
http://books.clubreading.com/book/bookdetail/book_id/2582
http://books.clubreading.com/book/bookdetail/book_id/2582
by: Philip K. Dick, entered by: Bill Estep>
Mon, 26 May 2008 21:28:50 +0000
As you can see, the document opens with the <channel>
tag and some information about the feed. Then the items repeat in the <item>
tag. For my sample document, I only used one item for simplicity.
Creating the RSS feed
The next decision to make was where to put the rss feed code? I made the decision to access the recent books feed from the index controller, so the final url will look like http://books.clubreading.com/index/rss . So, I created a created an empty view field rss.phtml in the application/views/scripts/index folder.
Next, in the IndexController, I added an rssAction function. To keep things simple, all the function needs to do is get a list of recently added books, format the feed data into an array, then dump the feed. Following Alex’s example, here’s what the final action looks like:
function rssAction()
{
// get recent books
$books = Book::recentbooks(3,0);
$baseUrl = 'http://books.clubreading.com';
<code>$tempbook = $books->current();
$pubDate = $tempbook->date_entered;
$feedArray = array(
'title' => 'ClubReading Books',
'link' => 'http://books.clubreading.com/index/rss',
'description' => 'An online community for the avid book lover.',
'language' => 'en-us',
'charset' => 'utf-8',
'pubDate' => $pubDate,
'generator' => 'Zend Framework Zend_Feed',
'entries' => array()
);
foreach ($books as $book) {
$feedArray['entries'][] = array(
'title' => $book->book_title,
'link' => $baseUrl . '/book/bookdetail/book_id/' . $book->book_id,
'guid' => $baseUrl . '/book/bookdetail/book_id/' . $book->book_id,
'description' => 'by: ' . $book->author . ', entered by: ' . $book->entered_by,
'pubDate' => $book->date_entered
);
}
$feed = Zend_Feed::importArray($feedArray, 'rss');
// Not needed - see comments
foreach ($feed as $entry) {
$element = $entry->summary->getDOM();
}
$feed->send();
</code>
}
The Result
Well, the new rssAction works like a champ! All that remained was to add the RSS widget to the wordpress sidebar and give it the url to the new rss feed.
I also added a subscribe icon to the Recent Books section of the Books website. After a quick google search, I found Feed Icons , and downloaded the publicly available Mozilla feed icons.
Like most programming adventures, there are as many different ways to implement a solution as there are programmers. :-) I don’t profess to implement any best practice…or even good code. But this worked for me, and maybe others will find it useful. Overall, a fun learning experience.