Introduction

Importing Feeds

Zend_Feed enables developers to retrieve feeds very easily. If you know the URI of a feed, simply use the Zend_Feed::import() method:

  1. span style="color: #ff0000;">'http://feeds.example.com/feedName');

You can also use Zend_Feed to fetch the contents of a feed from a file or the contents of a PHP string variable:

  1. // importing a feed from a text file
  2. 'feed.xml');
  3.  
  4. // importing a feed from a PHP string variable

In each of the examples above, an object of a class that extends Zend_Feed_Abstract is returned upon success, depending on the type of the feed. If an RSS feed were retrieved via one of the import methods above, then a Zend_Feed_Rss object would be returned. On the other hand, if an Atom feed were imported, then a Zend_Feed_Atom object is returned. The import methods will also throw a Zend_Feed_Exception object upon failure, such as an unreadable or malformed feed.

Custom feeds

Zend_Feed enables developers to create custom feeds very easily. You just have to create an array and to import it with Zend_Feed. This array can be imported with Zend_Feed::importArray() or with Zend_Feed::importBuilder(). In this last case the array will be computed on the fly by a custom data source implementing Zend_Feed_Builder_Interface.

Importing a custom array

  1. // importing a feed from an array
  2. // the following line is equivalent to the above;
  3. // by default a Zend_Feed_Atom instance is returned
  4. 'atom');
  5.  
  6. // importing a rss feed from an array
  7. 'rss');

The format of the array must conform to this structure:

  1. span style="color: #808080; font-style: italic;">//required
  2.     'title' => 'title of the feed',
  3.     'link'  => 'canonical url to the feed',
  4.  
  5.     // optional
  6.     'lastUpdate' => 'timestamp of the update date',
  7.     'published'  => 'timestamp of the publication date',
  8.  
  9.     // required
  10.     'charset' => 'charset of the textual data',
  11.  
  12.     // optional
  13.     'description' => 'short description of the feed',
  14.     'author'      => 'author/publisher of the feed',
  15.     'email'       => 'email of the author',
  16.  
  17.     // optional, ignored if atom is used
  18.     'webmaster' => 'email address for person responsible '
  19.                 .  'for technical issues',
  20.  
  21.     // optional
  22.     'copyright' => 'copyright notice',
  23.     'image'     => 'url to image',
  24.     'generator' => 'generator',
  25.     'language'  => 'language the feed is written in',
  26.  
  27.     // optional, ignored if atom is used
  28.     'ttl'    => 'how long in minutes a feed can be cached '
  29.              .  'before refreshing',
  30.     'rating' => 'The PICS rating for the channel.',
  31.  
  32.     // optional, ignored if atom is used
  33.     // a cloud to be notified of updates
  34.     'cloud'// required
  35.         'domain' => 'domain of the cloud, e.g. rpc.sys.com',
  36.  
  37.         // optional, defaults to 80
  38.         'port' => 'port to connect to',
  39.  
  40.         // required
  41.         'path'              => 'path of the cloud, e.g. /RPC2',
  42.         'registerProcedure' => 'procedure to call, e.g. myCloud.rssPlsNotify',
  43.         'protocol'          => 'protocol to use, e.g. soap or xml-rpc'
  44.     ),
  45.  
  46.     // optional, ignored if atom is used
  47.     // a text input box that can be displayed with the feed
  48.     'textInput'// required
  49.         'title'       => 'label of the Submit button in the text input area',
  50.         'description' => 'explains the text input area',
  51.         'name'        => 'the name of the text object in the text input area',
  52.         'link'        => 'URL of the CGI script processing text input requests'
  53.     ),
  54.  
  55.     // optional, ignored if atom is used
  56.     // Hint telling aggregators which hours they can skip
  57.     'skipHours'// up to 24 rows whose value is a number between 0 and 23
  58.         // e.g 13 (1pm)
  59.         'hour in 24 format'
  60.     ),
  61.  
  62.     // optional, ignored if atom is used
  63.     // Hint telling aggregators which days they can skip
  64.     'skipDays '// up to 7 rows whose value is
  65.         // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
  66.         // e.g Monday
  67.         'a day to skip'
  68.     ),
  69.  
  70.     // optional, ignored if atom is used
  71.     // Itunes extension data
  72.     'itunes'// optional, default to the main author value
  73.         'author' => 'Artist column',
  74.  
  75.         // optional, default to the main author value
  76.         // Owner of the podcast
  77.         'owner''name'  => 'name of the owner',
  78.             'email' => 'email of the owner'
  79.         ),
  80.  
  81.         // optional, default to the main image value
  82.         'image' => 'album/podcast art',
  83.  
  84.         // optional, default to the main description value
  85.         'subtitle' => 'short description',
  86.         'summary'  => 'longer description',
  87.  
  88.         // optional
  89.         'block' => 'Prevent an episode from appearing (yes|no)',
  90.  
  91.         // required, Category column and in iTunes Music Store Browse
  92.         'category'// up to 3 rows
  93. // required
  94.                 'main' => 'main category',
  95.  
  96.                 // optional
  97.                 'sub'  => 'sub category'
  98.             )
  99.         ),
  100.  
  101.         // optional
  102.         'explicit'     => 'parental advisory graphic (yes|no|clean)',
  103.         'keywords'     => 'a comma separated list of 12 keywords maximum',
  104.         'new-feed-url' => 'used to inform iTunes of new feed URL location'
  105.     ),
  106.  
  107.     'entries'//required
  108.             'title' => 'title of the feed entry',
  109.             'link'  => 'url to a feed entry',
  110.  
  111.             // required, only text, no html
  112.             'description' => 'short version of a feed entry',
  113.  
  114.             // optional
  115.             'guid' => 'id of the article, '
  116.                    .  'if not given link value will used',
  117.  
  118.             // optional, can contain html
  119.             'content' => 'long version',
  120.  
  121.             // optional
  122.             'lastUpdate' => 'timestamp of the publication date',
  123.             'comments'   => 'comments page of the feed entry',
  124.             'commentRss' => 'the feed url of the associated comments',
  125.  
  126.             // optional, original source of the feed entry
  127.             'source'// required
  128.                 'title' => 'title of the original source',
  129.                 'url'   => 'url of the original source'
  130.             ),
  131.  
  132.             // optional, list of the attached categories
  133.             'category'// required
  134.                     'term' => 'first category label',
  135.  
  136.                     // optional
  137.                     'scheme' => 'url that identifies a categorization scheme'// data for the second category and so on
  138.                 )
  139.             ),
  140.  
  141.             // optional, list of the enclosures of the feed entry
  142.             'enclosure'// required
  143.                     'url' => 'url of the linked enclosure',
  144.  
  145.                     // optional
  146.                     'type' => 'mime type of the enclosure',
  147.                     'length' => 'length of the linked content in octets'//data for the second enclosure and so on
  148. //data for the second entry and so on
  149.         )
  150.     )
  151. );

References:

Importing a custom data source

You can create a Zeed_Feed instance from any data source implementing Zend_Feed_Builder_Interface. You just have to implement the getHeader() and getEntries() methods to be able to use your object with Zend_Feed::importBuilder(). As a simple reference implementation, you can use Zend_Feed_Builder, which takes an array in its constructor, performs some minor validation, and then can be used in the importBuilder() method. The getHeader() method must return an instance of Zend_Feed_Builder_Header, and getEntries() must return an array of Zend_Feed_Builder_Entry instances.

Note: Zend_Feed_Builder serves as a concrete implementation to demonstrate the usage. Users are encouraged to make their own classes to implement Zend_Feed_Builder_Interface.

Here is an example of Zend_Feed::importBuilder() usage:

  1. // importing a feed from a custom builder source
  2. // the following line is equivalent to the above;
  3. // by default a Zend_Feed_Atom instance is returned
  4. 'atom');
  5.  
  6. // importing a rss feed from a custom builder array
  7. 'rss');

Dumping the contents of a feed

To dump the contents of a Zend_Feed_Abstract instance, you may use send() or saveXml() methods.

  1. span style="color: #808080; font-style: italic;">// dump the feed to standard output
  2. // send http headers and dump the feed

Introduction