Documentation

Introduction - Zend_Feed

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 #1 Putting Zend_Feed to Work on RSS Feed Data

  1. // Fetch the latest Slashdot headlines
  2. try {
  3.     $slashdotRss =
  4.         Zend_Feed::import('http://rss.slashdot.org/Slashdot/slashdot');
  5. } catch (Zend_Feed_Exception $e) {
  6.     // feed import failed
  7.     echo "Exception caught importing feed: {$e->getMessage()}\n";
  8.     exit;
  9. }
  10.  
  11. // Initialize the channel data array
  12. $channel = array(
  13.     'title'       => $slashdotRss->title(),
  14.     'link'        => $slashdotRss->link(),
  15.     'description' => $slashdotRss->description(),
  16.     'items'       => array()
  17.     );
  18.  
  19. // Loop over each channel item and store relevant data
  20. foreach ($slashdotRss as $item) {
  21.     $channel['items'][] = array(
  22.         'title'       => $item->title(),
  23.         'link'        => $item->link(),
  24.         'description' => $item->description()
  25.         );
  26. }

Previous topic

Zend_Feed

Next topic

Importing Feeds

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts