12.2. 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:

<?php
$feed = Zend_Feed::import('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:

<?php
// importing a feed from a text file
$feedFromFile = Zend_Feed::importFile('feed.xml');

// importing a feed from a PHP string variable
$feedFromPHP = Zend_Feed::importString($feedString);

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.

12.2.1. 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.

12.2.1.1. Importing a custom array

<?php
// importing a feed from an array
$atomFeedFromArray = Zend_Feed::importArray($array);

// the following line is equivalent to the above; by default a Zend_Feed_Atom instance is returned
$atomFeedFromArray = Zend_Feed::importArray($array, 'atom');

// importing a rss feed from an array
$rssFeedFromArray = Zend_Feed::importArray($array, 'rss');

The format of the array must conform to this structure:

<?php
array(
      'title'       => 'title of the feed', //required
      'link'        => 'canonical url to the feed', //required
      'lastUpdate'  => 'timestamp of the update date', // optional
      'published'   => 'timestamp of the publication date', //optional
      'charset'     => 'charset of the textual data', // required
      'description' => 'short description of the feed', //optional
      'author'      => 'author/publisher of the feed', //optional
      'email'       => 'email of the author', //optional
      'webmaster'   => 'email address for person responsible for technical issues' // optional, ignored if atom is used
      'copyright'   => 'copyright notice', //optional
      'image'       => 'url to image', //optional
      'generator'   => 'generator', // optional
      'language'    => 'language the feed is written in', // optional
      'ttl'         => 'how long in minutes a feed can be cached before refreshing', // optional, ignored if atom is used
      'rating'      => 'The PICS rating for the channel.', // optional, ignored if atom is used
      'cloud'       => array(
                             'domain'            => 'domain of the cloud, e.g. rpc.sys.com' // required
                             'port'              => 'port to connect to' // optional, default to 80
                             'path'              => 'path of the cloud, e.g. /RPC2' //required
                             'registerProcedure' => 'procedure to call, e.g. myCloud.rssPleaseNotify' // required
                             'protocol'          => 'protocol to use, e.g. soap or xml-rpc' // required
                             ), // a cloud to be notified of updates // optional, ignored if atom is used
      'textInput'   => array(
                             'title'       => 'the label of the Submit button in the text input area' // required,
                             'description' => 'explains the text input area' // required
                             'name'        => 'the name of the text object in the text input area' // required
                             'link'        => 'the URL of the CGI script that processes text input requests' // required
                             ) // a text input box that can be displayed with the feed // optional, ignored if atom is used
      'skipHours'   => array(
                             'hour in 24 format', // e.g 13 (1pm)
                             // up to 24 rows whose value is a number between 0 and 23
                             ) // Hint telling aggregators which hours they can skip // optional, ignored if atom is used
      'skipDays '   => array(
                             'a day to skip', // e.g Monday
                             // up to 7 rows whose value is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
                             ) // Hint telling aggregators which days they can skip // optional, ignored if atom is used
      'itunes'      => array(
                             'author'       => 'Artist column' // optional, default to the main author value
                             'owner'        => array(
                                                     'name' => 'name of the owner' // optional, default to main author value
                                                     'email' => 'email of the owner' // optional, default to main email value
                                                     ) // Owner of the podcast // optional
                             'image'        => 'album/podcast art' // optional, default to the main image value
                             'subtitle'     => 'short description' // optional, default to the main description value
                             'summary'      => 'longer description' // optional, default to the main description value
                             'block'        => 'Prevent an episode from appearing (yes|no)' // optional
                             'category'     => array(
                                                     array('main' => 'main category', // required
                                                           'sub'  => 'sub category' // optional
                                                           ),
                                                     // up to 3 rows
                                                     ) // 'Category column and in iTunes Music Store Browse' // required
                             'explicit'     => 'parental advisory graphic (yes|no|clean)' // optional
                             'keywords'     => 'a comma separated list of 12 keywords maximum' // optional
                             'new-feed-url' => 'used to inform iTunes of new feed URL location' // optional
                             ) // Itunes extension data // optional, ignored if atom is used
      'entries'     => array(
                             array(
                                   'title'        => 'title of the feed entry', //required
                                   'link'         => 'url to a feed entry', //required
                                   'description'  => 'short version of a feed entry', // only text, no html, required
                                   'guid'         => 'id of the article, if not given link value will used', //optional
                                   'content'      => 'long version', // can contain html, optional
                                   'lastUpdate'   => 'timestamp of the publication date', // optional
                                   'comments'     => 'comments page of the feed entry', // optional
                                   'commentRss'   => 'the feed url of the associated comments', // optional
                                   'source'       => array(
                                                           'title' => 'title of the original source' // required,
                                                           'url' => 'url of the original source' // required
                                                           ) // original source of the feed entry // optional
                                   'category'     => array(
                                                           array(
                                                                 'term' => 'first category label' // required,
                                                                 'scheme' => 'url that identifies a categorization scheme' // optional
                                                                 ),
                                                           array(
                                                                 //data for the second category and so on
                                                                 )
                                                           ) // list of the attached categories // optional
                                   'enclosure'    => array(
                                                           array(
                                                                 'url' => 'url of the linked enclosure' // required
                                                                 'type' => 'mime type of the enclosure' // optional
                                                                 'length' => 'length of the linked content in octets' // optional
                                                                 ),
                                                           array(
                                                                 //data for the second enclosure and so on
                                                                 )
                                                           ) // list of the enclosures of the feed entry // optional
                                   ),
                             array(
                                   //data for the second entry and so on
                                   )
                             )
       );

References:

12.2.1.2. 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.

[Nota] Nota

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:

<?php
// importing a feed from a custom builder source
$atomFeedFromArray = Zend_Feed::importBuilder(new Zend_Feed_Builder($array));

// the following line is equivalent to the above; by default a Zend_Feed_Atom instance is returned
$atomFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'atom');

// importing a rss feed from a custom builder array
$rssFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'rss');

12.2.1.3. Dumping the contents of a feed

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

<?php
assert($feed instanceof Zend_Feed_Abstract);

// dump the feed to standard output
print $feed->saveXML();

// send http headers and dump the feed
$feed->send();