Zend_Feed-ConsumingRss.xml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 RSS feed is as simple as instantiating a <classname>Zend_Feed_Rss</classname>
  7. object with the URL 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 RSS "channel" properties
  18. 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 XML object if they are requested with variable "getter" syntax
  26. (<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 PHP's array syntax:
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. echo $channel->category['domain'];
  35. ]]></programlisting>
  36. <para>
  37. Since XML attributes cannot have children, method syntax is not necessary for accessing
  38. attribute values.
  39. </para>
  40. <para>
  41. Most commonly you'll want to loop through the feed and do something with its entries.
  42. <classname>Zend_Feed_Abstract</classname> implements PHP's <code>Iterator</code> interface,
  43. so printing all titles of articles in a channel is just a matter of:
  44. </para>
  45. <programlisting language="php"><![CDATA[
  46. foreach ($channel as $item) {
  47. echo $item->title() . "\n";
  48. }
  49. ]]></programlisting>
  50. <para>
  51. If you are not familiar with RSS, here are the standard elements you can expect to be
  52. available in an RSS channel and in individual RSS items (entries).
  53. </para>
  54. <para>
  55. Required channel elements:
  56. </para>
  57. <para>
  58. <itemizedlist>
  59. <listitem>
  60. <para><code>title</code> - The name of the channel</para>
  61. </listitem>
  62. <listitem>
  63. <para>
  64. <code>link</code> - The URL of the web site corresponding to the channel
  65. </para>
  66. </listitem>
  67. <listitem>
  68. <para><code>description</code> - A sentence or several describing the channel</para>
  69. </listitem>
  70. </itemizedlist>
  71. </para>
  72. <para>
  73. Common optional channel elements:
  74. </para>
  75. <para>
  76. <itemizedlist>
  77. <listitem>
  78. <para>
  79. <code>pubDate</code> - The publication date of this set of content, in RFC 822
  80. date format
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para><code>language</code> - The language the channel is written in</para>
  85. </listitem>
  86. <listitem>
  87. <para>
  88. <code>category</code> - One or more (specified by multiple tags) categories the
  89. channel belongs to
  90. </para>
  91. </listitem>
  92. </itemizedlist>
  93. </para>
  94. <para>
  95. RSS <code>&lt;item&gt;</code> elements do not have any strictly required elements. However,
  96. either <code>title</code> or <code>description</code> must be present.
  97. </para>
  98. <para>
  99. Common item elements:
  100. </para>
  101. <para>
  102. <itemizedlist>
  103. <listitem>
  104. <para><code>title</code> - The title of the item</para>
  105. </listitem>
  106. <listitem>
  107. <para><code>link</code> - The URL of the item</para>
  108. </listitem>
  109. <listitem>
  110. <para><code>description</code> - A synopsis of the item</para>
  111. </listitem>
  112. <listitem>
  113. <para><code>author</code> - The author's email address</para>
  114. </listitem>
  115. <listitem>
  116. <para>
  117. <code>category</code> - One more more categories that the item belongs to
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para><code>comments</code> - URL of comments relating to this item</para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <code>pubDate</code> - The date the item was published, in RFC 822 date format
  126. </para>
  127. </listitem>
  128. </itemizedlist>
  129. </para>
  130. <para>
  131. In your code you can always test to see if an element is non-empty with:
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. if ($item->propname()) {
  135. // ... proceed.
  136. }
  137. ]]></programlisting>
  138. <para>
  139. If you use <code>$item->propname</code> instead, you will always get an empty object which
  140. will evaluate to <constant>TRUE</constant>, so your check will fail.
  141. </para>
  142. <para>
  143. For further information, the official RSS 2.0 specification is available at: <ulink
  144. url="http://blogs.law.harvard.edu/tech/rss">http://blogs.law.harvard.edu/tech/rss</ulink>
  145. </para>
  146. </sect1>
  147. <!--
  148. vim:se ts=4 sw=4 et:
  149. -->