The odd bit

Once is an accident, twice is a coincidence, three times is an enemy action.

The odd bit - Once is an accident, twice is a coincidence, three times is an enemy action.

HOWTO: Feed Component

Version 2007.1beta1 of the eZ Components has been released earlier this week. This release features a beta version of the Feed component. The downside is that there is almost no documentation available about this component, but I’m sure that is just a temporary situation that will be resolved before the final release.

Anyway, programmers are a bit like adventurers: to boldly go where no one has gone before. It would be a shame to keep this adventure for myself, so what follows is a small and simple tutorial that will show you how you can display items from an external feed.

There are two important classes: ezcFeed and ezcFeedItem. I don’t think they need any further explanation. A bit of Vulcan logic tells us we need to construct an object of ezcFeed. This example is about an external feed, so the ideal situation would be a method that creates an ezcFeed object from a URI. Fortunately, the components team agreed and created such a method:
$feed = ezcFeed::parse( '' );
Magic does its work and there’s our ezcFeed object (and internally an RSS 2.0 feed because that’s what the above link returns). It contains an instance of ezcFeedItem for every entry in the feed so we only need to retrieve that collection. I said something about displaying items, so we’ll throw in a template. I won’t be explaining the Template component because that’s beyond the scope of this tutorial. Here’s the deal:
$tpl = new ezcTemplate();
$tpl->send->feed_items = $feed->items;

So sometimes life can be easy! :-) Let’s display each item as an unordered list item. To create a hyperlink, we need a URI and some text. For items of an RSS 2.0 feed, we can use their title and link properties. This the template code:
{use $feed_items}
{foreach $feed_items as $feed_item}
<li><a href="{$feed_item->link}">{$feed_item->title}</a></li>

And that’s it! All that’s left beyond this point is to actually render the template and output the result. That’s nice and all but this code would fetch the feed on every page request so there’s room for improvement. I’ve wrapped the Cache component around the feed fetching. It all starts with the initialization of a cache:
$options = array(
'ttl' => 86400 // Refresh once a day
ezcCacheManager::createCache( 'feed', './var/cache/feed', 'ezcCacheStorageFilePlain', $options );

Now we need to fetch this cache instance and prepare the ID of our cache entry:
$feedCache = ezcCacheManager::getCache( 'feed' );
$cacheId = 'blog_hansmelis_be';

The scenario is to create the cache when we’ve fetched the feed from the external source. All following page requests will use the cached XML file until it has expired. To do so, we need to replace the single line ezcFeed::parse with:
if( ($feedData = $feedCache->restore( $cacheId )) === false )
$feed = ezcFeed::parse( '' );
$feedCache->store( $cacheId, $feed->generate() );
$feed = ezcFeed::parseContent( $feedData );

If the cache can’t be restored (it doesn’t exist or it has expired), we fetch the external feed and cache the XML data. If the cache has been restored, we use the XML data to build our ezcFeed instance.

So even though there’s little or no documentation for the Feed component, it’s still pretty easy to use and it can easily integrate with other components (Template and Cache in this example). While this is not breaking news, I hope it can help those looking for a simple start.

Your email address will not be published. Required fields are marked *