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