1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.feed.consuming-rss">
4 <title>Consuming an RSS Feed</title>
7 Reading an <acronym>RSS</acronym> feed is as simple as instantiating a
8 <classname>Zend_Feed_Rss</classname> object with the <acronym>URL</acronym> of the feed:
11 <programlisting language="php"><![CDATA[
12 $channel = new Zend_Feed_Rss('http://rss.example.com/channelName');
16 If any errors occur fetching the feed, a <classname>Zend_Feed_Exception</classname> will be
21 Once you have a feed object, you can access any of the standard <acronym>RSS</acronym>
22 "channel" properties directly on the object:
25 <programlisting language="php"><![CDATA[
26 echo $channel->title();
30 Note the function syntax. <classname>Zend_Feed</classname> uses a convention of treating
31 properties as <acronym>XML</acronym> object if they are requested with variable "getter"
32 syntax (<command>$obj->property</command>) and as strings if they are access with method
33 syntax (<command>$obj->property()</command>). This enables access to the full text of any
34 individual node while still allowing full access to all children.
38 If channel properties have attributes, they are accessible using <acronym>PHP</acronym>'s
42 <programlisting language="php"><![CDATA[
43 echo $channel->category['domain'];
47 Since <acronym>XML</acronym> attributes cannot have children, method syntax is not necessary
48 for accessing attribute values.
52 Most commonly you'll want to loop through the feed and do something with its entries.
53 <classname>Zend_Feed_Abstract</classname> implements <acronym>PHP</acronym>'s
54 <classname>Iterator</classname> interface, so printing all titles of articles in a channel
58 <programlisting language="php"><![CDATA[
59 foreach ($channel as $item) {
60 echo $item->title() . "\n";
65 If you are not familiar with <acronym>RSS</acronym>, here are the standard elements you can
66 expect to be available in an <acronym>RSS</acronym> channel and in individual
67 <acronym>RSS</acronym> items (entries).
71 Required channel elements:
76 <para><property>title</property> - The name of the channel</para>
81 <property>link</property> - The <acronym>URL</acronym> of the web site
82 corresponding to the channel
88 <property>description</property> - A sentence or several describing the channel
94 Common optional channel elements:
100 <property>pubDate</property> - The publication date of this set of content, in
101 <acronym>RFC</acronym> 822 date format
106 <para><property>language</property> - The language the channel is written in</para>
111 <property>category</property> - One or more (specified by multiple tags)
112 categories the channel belongs to
118 <acronym>RSS</acronym> <emphasis><item></emphasis> elements do not have any strictly
119 required elements. However, either <property>title</property> or
120 <property>description</property> must be present.
124 Common item elements:
129 <para><property>title</property> - The title of the item</para>
133 <para><property>link</property> - The <acronym>URL</acronym> of the item</para>
137 <para><property>description</property> - A synopsis of the item</para>
141 <para><property>author</property> - The author's email address</para>
146 <property>category</property> - One more categories that the item belongs to
152 <property>comments</property> - <acronym>URL</acronym> of comments relating to
159 <property>pubDate</property> - The date the item was published, in
160 <acronym>RFC</acronym> 822 date format
166 In your code you can always test to see if an element is non-empty with:
169 <programlisting language="php"><![CDATA[
170 if ($item->propname()) {
176 If you use <command>$item->propname</command> instead, you will always get an empty object
177 which will evaluate to <constant>TRUE</constant>, so your check will fail.
181 For further information, the official <acronym>RSS</acronym> 2.0 specification is available
183 url="http://blogs.law.harvard.edu/tech/rss">http://blogs.law.harvard.edu/tech/rss</ulink>