Chapter 20. Zend_Feed

Table of Contents

20.1. Introduction
20.2. Importing Feeds
20.2.1. Custom feeds
20.2.1.1. Importing a custom array
20.2.1.2. Importing a custom data source
20.2.1.3. Dumping the contents of a feed
20.3. Retrieving Feeds from Web Pages
20.4. Consuming an RSS Feed
20.5. Consuming an Atom Feed
20.6. Consuming a Single Atom Entry
20.7. Modifying Feed and Entry structures
20.8. Custom Feed and Entry Classes
20.9. Zend_Feed_Reader
20.9.1. Introduction
20.9.2. Importing Feeds
20.9.3. Retrieving Underlying Feed and Entry Sources
20.9.4. Cache Support and Intelligent Requests
20.9.4.1. Adding Cache Support to Zend_Feed_Reader
20.9.4.2. HTTP Conditional GET Support
20.9.5. Locating Feed URIs from Websites
20.9.6. Retrieving Feed Information
20.9.7. Retrieving Entry/Item Information
20.9.8. Extending Feed and Entry APIs
20.9.8.1. Writing Zend_Feed_Reader Extensions

20.1. Introduction

Zend_Feed provides functionality for consuming RSS and Atom feeds. It provides a natural syntax for accessing elements of feeds, feed attributes, and entry attributes. Zend_Feed also has extensive support for modifying feed and entry structure with the same natural syntax, and turning the result back into XML. In the future, this modification support could provide support for the Atom Publishing Protocol.

Programmatically, Zend_Feed consists of a base Zend_Feed class, abstract Zend_Feed_Abstract and Zend_Feed_Entry_Abstract base classes for representing Feeds and Entries, specific implementations of feeds and entries for RSS and Atom, and a behind-the-scenes helper for making the natural syntax magic work.

In the example below, we demonstrate a simple use case of retrieving an RSS feed and saving relevant portions of the feed data to a simple PHP array, which could then be used for printing the data, storing to a database, etc.

[Note] Be aware

Many RSS feeds have different channel and item properties available. The RSS specification provides for many optional properties, so be aware of this when writing code to work with RSS data.

Example 20.1. Putting Zend_Feed to Work on RSS Feed Data

// Fetch the latest Slashdot headlines
try {
    $slashdotRss =
        Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
} catch (Zend_Feed_Exception $e) {
    // feed import failed
    echo "Exception caught importing feed: {$e->getMessage()}\n";
    exit;
}

// Initialize the channel data array
$channel = array(
    'title'       => $slashdotRss->title(),
    'link'        => $slashdotRss->link(),
    'description' => $slashdotRss->description(),
    'items'       => array()
    );

// Loop over each channel item and store relevant data
foreach ($slashdotRss as $item) {
    $channel['items'][] = array(
        'title'       => $item->title(),
        'link'        => $item->link(),
        'description' => $item->description()
        );
}