| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.feed.consuming-rss">
- <title>Consuming an RSS Feed</title>
- <para>
- Reading an RSS feed is as simple as instantiating a <classname>Zend_Feed_Rss</classname>
- object with the URL of the feed:
- </para>
- <programlisting language="php"><![CDATA[
- $channel = new Zend_Feed_Rss('http://rss.example.com/channelName');
- ]]></programlisting>
- <para>
- If any errors occur fetching the feed, a <classname>Zend_Feed_Exception</classname> will be
- thrown.
- </para>
- <para>
- Once you have a feed object, you can access any of the standard RSS "channel" properties
- directly on the object:
- </para>
- <programlisting language="php"><![CDATA[
- echo $channel->title();
- ]]></programlisting>
- <para>
- Note the function syntax. <classname>Zend_Feed</classname> uses a convention of treating
- properties as XML object if they are requested with variable "getter" syntax
- (<code>$obj->property</code>) and as strings if they are access with method syntax
- (<code>$obj->property()</code>). This enables access to the full text of any individual node
- while still allowing full access to all children.
- </para>
- <para>
- If channel properties have attributes, they are accessible using PHP's array syntax:
- </para>
- <programlisting language="php"><![CDATA[
- echo $channel->category['domain'];
- ]]></programlisting>
- <para>
- Since XML attributes cannot have children, method syntax is not necessary for accessing
- attribute values.
- </para>
- <para>
- Most commonly you'll want to loop through the feed and do something with its entries.
- <classname>Zend_Feed_Abstract</classname> implements PHP's <code>Iterator</code> interface,
- so printing all titles of articles in a channel is just a matter of:
- </para>
- <programlisting language="php"><![CDATA[
- foreach ($channel as $item) {
- echo $item->title() . "\n";
- }
- ]]></programlisting>
- <para>
- If you are not familiar with RSS, here are the standard elements you can expect to be
- available in an RSS channel and in individual RSS items (entries).
- </para>
- <para>
- Required channel elements:
- </para>
- <para>
- <itemizedlist>
- <listitem>
- <para><code>title</code> - The name of the channel</para>
- </listitem>
- <listitem>
- <para>
- <code>link</code> - The URL of the web site corresponding to the channel
- </para>
- </listitem>
- <listitem>
- <para><code>description</code> - A sentence or several describing the channel</para>
- </listitem>
- </itemizedlist>
- </para>
- <para>
- Common optional channel elements:
- </para>
- <para>
- <itemizedlist>
- <listitem>
- <para>
- <code>pubDate</code> - The publication date of this set of content, in RFC 822
- date format
- </para>
- </listitem>
- <listitem>
- <para><code>language</code> - The language the channel is written in</para>
- </listitem>
- <listitem>
- <para>
- <code>category</code> - One or more (specified by multiple tags) categories the
- channel belongs to
- </para>
- </listitem>
- </itemizedlist>
- </para>
- <para>
- RSS <code><item></code> elements do not have any strictly required elements. However,
- either <code>title</code> or <code>description</code> must be present.
- </para>
- <para>
- Common item elements:
- </para>
- <para>
- <itemizedlist>
- <listitem>
- <para><code>title</code> - The title of the item</para>
- </listitem>
- <listitem>
- <para><code>link</code> - The URL of the item</para>
- </listitem>
- <listitem>
- <para><code>description</code> - A synopsis of the item</para>
- </listitem>
- <listitem>
- <para><code>author</code> - The author's email address</para>
- </listitem>
- <listitem>
- <para>
- <code>category</code> - One more more categories that the item belongs to
- </para>
- </listitem>
- <listitem>
- <para><code>comments</code> - URL of comments relating to this item</para>
- </listitem>
- <listitem>
- <para>
- <code>pubDate</code> - The date the item was published, in RFC 822 date format
- </para>
- </listitem>
- </itemizedlist>
- </para>
- <para>
- In your code you can always test to see if an element is non-empty with:
- </para>
- <programlisting language="php"><![CDATA[
- if ($item->propname()) {
- // ... proceed.
- }
- ]]></programlisting>
- <para>
- If you use <code>$item->propname</code> instead, you will always get an empty object which
- will evaluate to <constant>TRUE</constant>, so your check will fail.
- </para>
- <para>
- For further information, the official RSS 2.0 specification is available at: <ulink
- url="http://blogs.law.harvard.edu/tech/rss">http://blogs.law.harvard.edu/tech/rss</ulink>
- </para>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|