Jan 20 2011

Zend Framework : Consuming feeds using Zend_Feed

Using Zend Framework Zend_Feed is extremely simple to iteract or even modify a rss feed. Let’s give a quick look into consuming feeds.

<?php
$feedUrl = "http://blog.josedasilva.net/feed/";
 
// Lets import the feed
try {
     $feed   =   Zend_Feed::import($feedUrl);
} catch (Zend_Feed_Exception $e) {
     echo 'Exception found: '.$e->getMessage();
}
 
// Iterating over the feed
foreach($feed as $index=>$entry) {
    echo (string)$entry->title."\n";
    echo (string)$entry->description."\n";
}
 
// Playing around with the complete feed and outputting it.
 
ob_start();
$feed->send();
$feedContent =   ob_get_contents(); // feed content stored on this variable
ob_end_clean();
 
echo $feedContent;
?>

Some comments

$feed = Zend_Feed::import($feedUrl);
This will import the feed from a feed url and return an object of a class that extends Zend_Feed_Abstract if the importation has success and throws a Zend_Feed_Exception if it’s not.

foreach($feed as $index=>$entry)

We may iterate over a feed object using a foreach, on my example you will access each feed entry on the $entry variable.

$entry var will be a Zend_Feed_Entry_Rss or a Zend_Feed_Entry_Atom object instance, depending on the feed type you have imported. Each $entry attribute (title, description , …) will be an Zend_Feed_Element object instance, although you can simply to access the attribute value by forcing a cast into (string).

Now, this is a quick look into reading data from a RSS feed using Zend_Feed, on the next post on this series i will give a quick insights on modifying the feed and returning the result.

One comment on “Zend Framework : Consuming feeds using Zend_Feed

  1. Pingback: » Zend Framework: Modifying rss feeds using Zend_Feed : Professional PHP Developer – Jose da Silva

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>