12.4. Consuming an RSS Feed

Reading an RSS feed is as simple as instantiating a Zend_Feed_Rss object with the URL of the feed:

<?php

$channel = new Zend_Feed_Rss('http://rss.example.com/channelName');

?>

If any errors occur fetching the feed, a Zend_Feed_Exception will be thrown.

Once you have a feed object, you can access any of the standard RSS "channel" properties directly on the object:

<?php

echo $channel->title();

?>

Note the function syntax. Zend_Feed uses a convention of treating properties as XML object if they are requested with variable "getter" syntax ($obj->property) and as strings if they are access with method syntax ($obj->property()). This enables access to the full text of any individual node while still allowing full access to all children.

If channel properties have attributes, they are accessible using PHP's array syntax:

<?php

echo $channel->category['domain'];

?>

Since XML attributes cannot have children, method syntax is not necessary for accessing attribute values.

Most commonly you'll want to loop through the feed and do something with its entries. Zend_Feed_Abstract implements PHP's Iterator interface, so printing all titles of articles in a channel is just a matter of:

<?php

foreach ($channel as $item) {
    echo $item->title() . "\n";
}

?>

If you are not familiar with RSS, here are the standard elements you can expect to be available in an RSS channel and in individual RSS items (entries).

Required channel elements:

Common optional channel elements:

RSS <item> elements do not have any strictly required elements. However, either title or description must be present.

Common item elements:

In your code you can always test to see if an element is non-empty with:

<?php

if ($item->propname()) {
    // ... proceed.
}

?>

If you use $item->propname instead, you will always get an empty object which will evaluate to TRUE, so your check will fail.

For further information, the official RSS 2.0 specification is available at: http://blogs.law.harvard.edu/tech/rss