Zend_Feed-ConsumingRss.xml 5.2 KB

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