RSS feed in Zend Framework

Univac Computer

Photo credit: cliff1066

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 ` tag, for example,Bold 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 text

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:

http://books.clubreading.com/index/rss An online community for the avid book lover.>

Mon, 26 May 2008 21:28:50 +0000 Zend Framework ZendFeed
en-us

http://blogs.law.harvard.edu/tech/rss

http://books.clubreading.com/book/bookdetail/bookid/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 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.

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’;

$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();

}

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.

11 Responses to “RSS feed in Zend Framework”

  1. Smith 21. Jul, 2008 at 6:56 pm #

    Question: Why do you do this?

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

    Also, it looks like the zend feed requirements changed. The date must now be entered as ‘lastUpdate’ instead of ‘pubDate’, which it gets converted to in the created feed, so its kinda counter intuitive.

  2. bestep 21. Jul, 2008 at 11:19 pm #

    Hi Smith,

    I had the

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

    because the example I was trying to model from was working from the other direction – absorbing a feed, as opposed to creating a feed. Great catch. It isn’t needed…just a wasted walk through the feed. I’ll updated the code example accordingly.

    Unless I’m mistaken, the ‘lastUpdate’ (lastBuildDate in RSS 2.0 standard) is the modified date of an element, and ‘pubDate’ is the date of the rss publication (current date). The three are all optional, but still part of the RSS 2.0 standard, (http://cyber.law.harvard.edu/rss/rss.html).

    Let me know if I have that wrong…I’d like to read more about the expected or optional elements.

    Thanks,

    Bill

  3. Smith 22. Jul, 2008 at 12:04 am #

    Thanks for replying. What I experienced was bizarre and illogical, but setting the date in pubDate would never set the date I wanted; it would set the current date and time instead, so there was a problem obviously. I went to the Zend website ( http://framework.zend.com/manual/en/zend.feed.importing.html ) and I didnt see pubDate at all in the Custom Feeds section. Instead this was shown for each entry record.

    ‘lastUpdate’ => ‘timestamp of the publication date’, // optional

    So thats what I have done, and in my feed, it used the value for lastUpdate, and renamed the field pubDate. So it works; when creating a feed you must do this:

    $feedArray['entries'][] = array(
    ‘title’ => $book->booktitle,
    ‘link’ => $baseUrl . ‘/book/bookdetail/book
    id/’ . $book->bookid,
    ‘guid’ => $baseUrl . ‘/book/bookdetail/book
    id/’ . $book->bookid,
    ‘description’ => ‘by: ‘ . $book->author . ‘, entered by: ‘ . $book->entered
    by,
    ‘lastUpdate’ => $book->date_entered
    );

  4. bestep 22. Jul, 2008 at 3:46 pm #

    That makes sense. I initially made a bad assumption. I was stuck in the old-days when rss feed files were generated manually, but with Zend_Feed, we’re creating the feed on the fly with live data. So the pubDate doesn’t make sense in this case.

    I think your idea is a great one. Why not flag each element with the pub date of the element. Makes sense to me. I’m not, in this case, using the date for anything so it doesn’t matter much. But if this were a news feed, it would make a lot of sense.

    Thanks for the great comments Smith.

  5. Jyotsna 07. Nov, 2008 at 5:19 pm #

    Hi,

    I got the result with no formating, that too only link and Date. Can you explain how to solve this?

    Thanks,
    Jyotsna.

  6. Jyotsna 20. Nov, 2008 at 5:23 pm #

    Hi again,

    I got it worked.

    First and most important part is: in the RSSController class constructor, You SHOULD add this line

    $this->_helper->viewRenderer->setNoRender();

    Regards,
    Jyotsna.

  7. gyongyeee 07. Jan, 2009 at 2:00 pm #

    If you are using Zend_Layout, don’t forget to disable it also for this action:

            $feed->send();        $this->_helper->viewRenderer->setNoRender();        $this->_helper->layout->disableLayout();

  8. Hunter 14. Mar, 2009 at 12:15 am #

    Hi bestep,

    thank you for this tutorial. Helps a lot.

    However, I have a problem when adding new tags, using a namespace (eg dc or average).

    I’m trying to do this:

    #title#
    #link#
    #description#
    #language#
    #copyright#
    #pubDate#
    #lastBuildDate#
    #docs#

    #imageurl#
    #image
    title#
    #image_link#

        <item>        <title>#title#</title>        <link>#link#</link>        <guid isPermaLink="true">#link#</guid>        <media:content url="#image_url#" medium="image" height="#image_height#" width="#image_width#" />        <media:description>#image_description#</media:description>        <media:credit>#image_author#</media:credit>        <category>#categoria#</category>        <pubDate>#pubDate#</pubDate>        <dc:creator>#creator#</dc:creator>    </item>

    </channel>

    ////

    I have problem to add this tags:

    #imagedescription#
    #image
    author#
    #creator#

    I do not know how to add the namespaces to do this, please help me.

    Thank you very much from Chile

  9. marcis 20. May, 2009 at 12:36 pm #

    The empty view field (rss.phtml) is not necessary. Just add an “exit” call at the end of the method:

      ...  $feed->send();  exit;

    }

    Thanks!

  10. Eddie 01. Jun, 2009 at 6:19 am #

    Excellent!

    Thanks for the tutorial & all the comments. I managed to put a feed together v.quickly.

    1 thing I did notice that might help others and is not mentioned here is, I got an error “…DOMDocument.php failed to open stream…”, yet all my include paths were there and correct. After some googling I did find that i needed to install php-xml. I did this using yum…

    yum install php-xml

    and hey presto it worked.

    Hope that helps someone :)

  11. Sexcambabez 02. Sep, 2009 at 8:12 pm #

    Hier sind die geilen und hemmungslosen Girls die den http://www.sexcamamateure.net“ rel=”nofollow”>Livesex privat von zu hause zeigen.
    Geil und hemmungslos treiben Sie es dabei mit Dir vor der Webcam und Du bestimmst einfach ob Du nur die geilen
    Titten sehen moechtest, oder die schoen rasierte Muschi , oder ob Du sogar knallhartenhttp://www.sexcamamateure.net“ rel=”nofollow”> Sex mit der Frau erleben willst.
    Hier ist das Portal wo private Sexgirls alles zeigen was Du moechtest, Anonym und absolute Amateure. Geile Action
    mit den Girls die Dir beim Livesex von zu Hause aus auch noch den letzten Tropfen rausholen, geniesse diehttp://www.sexcamamateure.net“ rel=”nofollow”> Sexcam Girls nun ganz privat.
    Viel Spass und vor allem ein geiles Abaenteuer!!!

Leave a Reply