March 1, 2021
Hot Topics:

Creating RSS Feeds with the Zend Framework for Fun and Profit

  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Over the past few years, I've shown you how to build custom RSS aggregators using PHP and MySQL and Ruby. Even five years after penning the former tutorial, RSS is going as strong as ever, depended upon by millions of users around the globe to keep track of an increasingly diverse array of online information. For instance, back in that 2004 tutorial I praised websites such as Yahoo!, CNET, and The Christian Science Monitor for their efforts to bring RSS to a larger population. Five years later, RSS feeds have become as commonplace as contact forms, and in fact their application has expanded to reading email, keeping tabs on the weather, and following your friends on Twitter. So, if you haven't already taken steps to integrate RSS feeds into your website, what's stopping you? You're not only denying your existing users a great mechanism for keeping tabs on your website's latest updates, but are potentially hindering audience growth by turning visitors insistent on cutting down on tedious repeat visitations to countless websites.

Still not convinced? What if I told you there's not only an incredibly easy way to build RSS feeds from your custom data, but also about a great solution for making money by integrating advertising directly into these feeds? By using the Zend Framework and Google AdSense, accomplishing both tasks is frighteningly easy. In this tutorial, I'll show you how.

Creating Custom Feeds with the Zend Framework

As you've seen in previous tutorials, the Zend Framework (see Twitter, internationalization, Amazon Web Services, and OpenID Authentication tutorials) offers Web developers a tremendously powerful solution not only for building maintainable websites, but also for taking advantage of the latest web services. This offering includes the Zend_Feed component, which makes it incredibly easy to both build and consume RSS feeds.

For instance, I use Zend_Feed in conjunction with regularly executing cron job to create and RSS feed highlighting the 25 most popular video games according to Amazon.com sales. Using wget and HTTP authentication, every six hours the cron job executes, using curl to contact a URL that triggers the building of an updated RSS feed. The crontab entry looks like this:

0 0,6,12,18 * * * /usr/bin/curl --user webuser:supersecret
   --silent http://www.gamenomad.com/cronjobs/populargames

This entry tells cron to execute every day of every month at 12am, 6am, 12pm, and 6pm, using the curl program to contact the provided GameNomad URL. Because I don't want just anybody contacting this URL, it's password-protected. Authentication occurs using curl's --user option. See Wikipedia's cron entry for more information about cron and crontab.

The populargames action found in the cronjobs controller is presented next; it takes advantage of the Zend_Feed component to build the feed based on information stored in the website's MySQL database. Rather than exhaustively guide you through every line of the action, I've included line numbers alongside the code, and will follow the listing with a breakdown of key lines.

01 public function populargamesAction()
02 {
04     // Create array to store the RSS feed entries
05     $entries = array();
07     // Retrieve the 25 most popular games
08     $game = new Game();
09     $rankings = $game->getLatestSalesRanks(25);
11     // Cycle through the rankings, creating an array storing
12     // each, and push the array onto the $entries array
13     foreach ($rankings AS $ranking) {
14         $entry = array(
15           'title'       => "{$ranking->title}
16           'link'        => "http://www.gamenomad.com/games/
17           'description' => "Sales Rank: #{$ranking->rank}",
18         );
19         array_push($entries, $entry);
20     }
22     // Create the RSS array
23     $rss = array(
24       'title'   => 'GameNomad: Popular Games',
25       'link'    => 'http://www.gamenomad.com/games/ranks',
26       'charset' => 'ISO-8859-1',
27       'entries' => $entries
28     );
30     // Import the array
31     $feed = Zend_Feed::importArray($rss, 'rss');
33     // Write the feed to a variable
34     $rssFeed = $feed->saveXML();
36     // Write the feed to a file residing in /public/rss
37     $fh = fopen($this->config->feed->popular_games, "w");
38     fwrite($fh, $rssFeed);
39     fclose($fh);
41 }

If you're even moderately familiar with the Zend Framework, much of this should be quite familiar, save for the Zend_Feed-specific calls. Now, review key lines in this listing:

  • Line 09: Retrieve an array consisting of the top 25 sales ranks, using a method defined within the Game model.
  • Lines 13-20: Cycle through the array, creating an associative array consisting of each game's title, platform, GameNomad URL, and latest sales rank. Line 19 will push this array onto another array, which will contain each newly created associative array.
  • Lines 23-27: Define the RSS feeds title, URL, and character set.
  • Line 28: Assign the $entries array to the entries index of the $rss array, which will result in each element of the $entries array being defined as an RSS item.
  • Line 31: Generate the RSS feed.
  • Line 34: Save the feed to a variable.
  • Lines 37-39: Write the feed to a publicly-accessible file.

Incidentally, you can access this feed by navigating to the following location:


Page 1 of 3

This article was originally published on February 26, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date