Creating a Simple RSS Feed in Zend Framework

Here’s a post from the old days. This first went up on 5/26/2008. Enjoy!

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”.

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.

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:

&lt;code&gt; Description text
<pre><code>&lt;code&gt; Description text

&lt;/code&gt;

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:

As you can see, the document opens with the tag and some information about the feed. Then the items repeat in the tag. For my sample document, I only used one item for simplicity.

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()
<pre><code>function rssAction()
{
    // get recent books
    $books = Book::recentbooks(3,0);
    $baseUrl = 'http://books.clubreading.com';

    $tempbook = $books-&gt;current();
    $pubDate = $tempbook-&gt;date_entered;

    $feedArray = array(
                'title' =&gt; 'ClubReading Books',
                'link' =&gt; 'http://books.clubreading.com/index/rss',
                'description' =&gt; 'An online community for the avid book lover.',
                'language' =&gt; 'en-us',
                'charset' =&gt; 'utf-8',
                'pubDate' =&gt; $pubDate,
                'generator' =&gt; 'Zend Framework Zend_Feed',
                'entries' =&gt; array()
            );

    foreach ($books as $book) {
            $feedArray['entries'][] = array(
                'title' =&gt; $book-&gt;book_title,
                'link' =&gt; $baseUrl . '/book/bookdetail/book_id/' . $book-&gt;book_id,
                'guid' =&gt; $baseUrl . '/book/bookdetail/book_id/' . $book-&gt;book_id,
                'description' =&gt; 'by: ' . $book-&gt;author . ', entered by: ' . $book-&gt;entered_by,
                'pubDate' =&gt; $book-&gt;date_entered
                );
            }

    $feed = Zend_Feed::importArray($feedArray, 'rss');

    foreach ($feed as $entry) {
        $element = $entry-&gt;summary-&gt;getDOM();
    }

    $feed-&gt;send();
}

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.

Return to blog