Zend_Feed-ConsumingRss.xml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 (<command>$obj->property</command>) and as strings if they are access with method
  27. syntax (<command>$obj->property()</command>). This enables access to the full text of any
  28. individual node 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. <classname>Iterator</classname> interface, so printing all titles of articles in a channel
  45. is just a 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. <itemizedlist>
  61. <listitem>
  62. <para><property>title</property> - The name of the channel</para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. <property>link</property> - The <acronym>URL</acronym> of the web site
  67. corresponding to the channel
  68. </para>
  69. </listitem>
  70. <listitem>
  71. <para>
  72. <property>description</property> - A sentence or several describing the channel
  73. </para>
  74. </listitem>
  75. </itemizedlist>
  76. <para>
  77. Common optional channel elements:
  78. </para>
  79. <itemizedlist>
  80. <listitem>
  81. <para>
  82. <property>pubDate</property> - The publication date of this set of content, in
  83. <acronym>RFC</acronym> 822 date format
  84. </para>
  85. </listitem>
  86. <listitem>
  87. <para><property>language</property> - The language the channel is written in</para>
  88. </listitem>
  89. <listitem>
  90. <para>
  91. <property>category</property> - One or more (specified by multiple tags)
  92. categories the channel belongs to
  93. </para>
  94. </listitem>
  95. </itemizedlist>
  96. <para>
  97. <acronym>RSS</acronym> <emphasis>&lt;item&gt;</emphasis> elements do not have any strictly
  98. required elements. However, either <property>title</property> or
  99. <property>description</property> must be present.
  100. </para>
  101. <para>
  102. Common item elements:
  103. </para>
  104. <itemizedlist>
  105. <listitem>
  106. <para><property>title</property> - The title of the item</para>
  107. </listitem>
  108. <listitem>
  109. <para><property>link</property> - The <acronym>URL</acronym> of the item</para>
  110. </listitem>
  111. <listitem>
  112. <para><property>description</property> - A synopsis of the item</para>
  113. </listitem>
  114. <listitem>
  115. <para><property>author</property> - The author's email address</para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <property>category</property> - One more categories that the item belongs to
  120. </para>
  121. </listitem>
  122. <listitem>
  123. <para>
  124. <property>comments</property> - <acronym>URL</acronym> of comments relating to
  125. this item
  126. </para>
  127. </listitem>
  128. <listitem>
  129. <para>
  130. <property>pubDate</property> - The date the item was published, in
  131. <acronym>RFC</acronym> 822 date format
  132. </para>
  133. </listitem>
  134. </itemizedlist>
  135. <para>
  136. In your code you can always test to see if an element is non-empty with:
  137. </para>
  138. <programlisting language="php"><![CDATA[
  139. if ($item->propname()) {
  140. // ... proceed.
  141. }
  142. ]]></programlisting>
  143. <para>
  144. If you use <command>$item->propname</command> instead, you will always get an empty object
  145. which will evaluate to <constant>TRUE</constant>, so your check will fail.
  146. </para>
  147. <para>
  148. For further information, the official <acronym>RSS</acronym> 2.0 specification is available
  149. at: <ulink
  150. url="http://blogs.law.harvard.edu/tech/rss">http://blogs.law.harvard.edu/tech/rss</ulink>
  151. </para>
  152. </sect1>
  153. <!--
  154. vim:se ts=4 sw=4 et:
  155. -->