Zend_Feed-ConsumingRss.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.consuming-rss">
  4. <title>Consuming an RSS Feed</title>
  5. <para>
  6. Reading an <acronym>RSS</acronym> feed is as simple as instantiating a
  7. <classname>Zend_Feed_Rss</classname> object with the <acronym>URL</acronym> of the feed:
  8. </para>
  9. <programlisting language="php"><![CDATA[
  10. $channel = new Zend_Feed_Rss('http://rss.example.com/channelName');
  11. ]]></programlisting>
  12. <para>
  13. If any errors occur fetching the feed, a <classname>Zend_Feed_Exception</classname> will be
  14. thrown.
  15. </para>
  16. <para>
  17. Once you have a feed object, you can access any of the standard <acronym>RSS</acronym>
  18. "channel" properties directly on the object:
  19. </para>
  20. <programlisting language="php"><![CDATA[
  21. echo $channel->title();
  22. ]]></programlisting>
  23. <para>
  24. Note the function syntax. <classname>Zend_Feed</classname> uses a convention of treating
  25. properties as <acronym>XML</acronym> object if they are requested with variable "getter"
  26. syntax (<code>$obj->property</code>) and as strings if they are access with method syntax
  27. (<code>$obj->property()</code>). This enables access to the full text of any individual node
  28. while still allowing full access to all children.
  29. </para>
  30. <para>
  31. If channel properties have attributes, they are accessible using <acronym>PHP</acronym>'s
  32. array syntax:
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. echo $channel->category['domain'];
  36. ]]></programlisting>
  37. <para>
  38. Since <acronym>XML</acronym> attributes cannot have children, method syntax is not necessary
  39. for accessing attribute values.
  40. </para>
  41. <para>
  42. Most commonly you'll want to loop through the feed and do something with its entries.
  43. <classname>Zend_Feed_Abstract</classname> implements <acronym>PHP</acronym>'s
  44. <code>Iterator</code> interface, so printing all titles of articles in a channel is just a
  45. matter of:
  46. </para>
  47. <programlisting language="php"><![CDATA[
  48. foreach ($channel as $item) {
  49. echo $item->title() . "\n";
  50. }
  51. ]]></programlisting>
  52. <para>
  53. If you are not familiar with <acronym>RSS</acronym>, here are the standard elements you can
  54. expect to be available in an <acronym>RSS</acronym> channel and in individual
  55. <acronym>RSS</acronym> items (entries).
  56. </para>
  57. <para>
  58. Required channel elements:
  59. </para>
  60. <para>
  61. <itemizedlist>
  62. <listitem>
  63. <para><code>title</code> - The name of the channel</para>
  64. </listitem>
  65. <listitem>
  66. <para>
  67. <code>link</code> - The <acronym>URL</acronym> of the web site corresponding to
  68. the channel
  69. </para>
  70. </listitem>
  71. <listitem>
  72. <para><code>description</code> - A sentence or several describing the channel</para>
  73. </listitem>
  74. </itemizedlist>
  75. </para>
  76. <para>
  77. Common optional channel elements:
  78. </para>
  79. <para>
  80. <itemizedlist>
  81. <listitem>
  82. <para>
  83. <code>pubDate</code> - The publication date of this set of content, in
  84. <acronym>RFC</acronym> 822 date format
  85. </para>
  86. </listitem>
  87. <listitem>
  88. <para><code>language</code> - The language the channel is written in</para>
  89. </listitem>
  90. <listitem>
  91. <para>
  92. <code>category</code> - One or more (specified by multiple tags) categories the
  93. channel belongs to
  94. </para>
  95. </listitem>
  96. </itemizedlist>
  97. </para>
  98. <para>
  99. <acronym>RSS</acronym> <code>&lt;item&gt;</code> elements do not have any strictly required
  100. elements. However, either <code>title</code> or <code>description</code> must be present.
  101. </para>
  102. <para>
  103. Common item elements:
  104. </para>
  105. <para>
  106. <itemizedlist>
  107. <listitem>
  108. <para><code>title</code> - The title of the item</para>
  109. </listitem>
  110. <listitem>
  111. <para><code>link</code> - The <acronym>URL</acronym> of the item</para>
  112. </listitem>
  113. <listitem>
  114. <para><code>description</code> - A synopsis of the item</para>
  115. </listitem>
  116. <listitem>
  117. <para><code>author</code> - The author's email address</para>
  118. </listitem>
  119. <listitem>
  120. <para>
  121. <code>category</code> - One more categories that the item belongs to
  122. </para>
  123. </listitem>
  124. <listitem>
  125. <para>
  126. <code>comments</code> - <acronym>URL</acronym> of comments relating to this item
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <code>pubDate</code> - The date the item was published, in
  132. <acronym>RFC</acronym> 822 date format
  133. </para>
  134. </listitem>
  135. </itemizedlist>
  136. </para>
  137. <para>
  138. In your code you can always test to see if an element is non-empty with:
  139. </para>
  140. <programlisting language="php"><![CDATA[
  141. if ($item->propname()) {
  142. // ... proceed.
  143. }
  144. ]]></programlisting>
  145. <para>
  146. If you use <code>$item->propname</code> instead, you will always get an empty object which
  147. will evaluate to <constant>TRUE</constant>, so your check will fail.
  148. </para>
  149. <para>
  150. For further information, the official <acronym>RSS</acronym> 2.0 specification is available
  151. at: <ulink
  152. url="http://blogs.law.harvard.edu/tech/rss">http://blogs.law.harvard.edu/tech/rss</ulink>
  153. </para>
  154. </sect1>
  155. <!--
  156. vim:se ts=4 sw=4 et:
  157. -->