Zend_Feed-Importing.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <sect1 id="zend.feed.importing">
  2. <title> 导入Feeds </title>
  3. <para>
  4. <code>Zend_Feed</code> 能让开发者轻松获得 Feeds 。如果你知道 Feeds 的URI,用<code>Zend_Feed::import()</code>方法就可以非常容易的获得它:
  5. </para>
  6. <programlisting role="php"><![CDATA[<?php
  7. $feed = Zend_Feed::import('http://feeds.example.com/feedName');]]>
  8. </programlisting>
  9. <para>
  10. 你也能用<code>Zend_Feed</code>从一个文件或者一个PHP字符串变量来获得一个feed的内容:
  11. </para>
  12. <programlisting role="php"><![CDATA[<?php
  13. // importing a feed from a text file
  14. $feedFromFile = Zend_Feed::importFile('feed.xml');
  15. // importing a feed from a PHP string variable
  16. $feedFromPHP = Zend_Feed::importString($feedString);]]>
  17. </programlisting>
  18. <para>
  19. 在上面的例子中,根据feed类型的不同,一个从<code>Zend_Feed_Abstract</code>继承而来的类对象被返回。如果导入方法获得的是一个RSS feed,那么一个<code>Zend_Feed_Rss</code>对象将被返回(Seateng译注:以Factory模式实现)。另一方面,如果一个Atom feed被导入,那么将返回一个<code>Zend_Feed_Atom</code>对象。如果feed不可读或者不符合规范,导致导入失败那么Zend_Feed将抛出一个<code>Zend_Feed_Exception</code>异常。
  20. </para>
  21. <sect2 id="zend.feed.importing.custom">
  22. <title> 定制 feeds </title>
  23. <para>
  24. <code>Zend_Feed</code> 能让开发者轻松创建定制 feeds,只需要创建一个数组用 Zend_Feed 导入它。这个数组可以用 <code>Zend_Feed::importArray()</code> 或 <code>Zend_Feed::importBuilder()</code> 导入。最后数组用定制的实现了 <code>Zend_Feed_Builder_Interface</code> 数据源来处理。
  25. </para>
  26. <sect3 id="zend.feed.importing.custom.importarray">
  27. <title> 导入定制的数组 </title>
  28. <programlisting role="php"><![CDATA[<?php
  29. // 从数组导入 feed
  30. $atomFeedFromArray = Zend_Feed::importArray($array);
  31. // 下面一行和上面相同;缺省地 Zend_Feed_Atom 实例被返回
  32. $atomFeedFromArray = Zend_Feed::importArray($array, 'atom');
  33. // 从数组导入 rss feed
  34. $rssFeedFromArray = Zend_Feed::importArray($array, 'rss');]]>
  35. </programlisting>
  36. <para>
  37. 数组格式必须和这个结构一致:
  38. </para>
  39. <programlisting role="php"><![CDATA[<?php
  40. array(
  41. 'title' => 'title of the feed', //required
  42. 'link' => 'canonical url to the feed', //required
  43. 'lastUpdate' => 'timestamp of the update date', // optional
  44. 'published' => 'timestamp of the publication date', //optional
  45. 'charset' => 'charset of the textual data', // required
  46. 'description' => 'short description of the feed', //optional
  47. 'author' => 'author/publisher of the feed', //optional
  48. 'email' => 'email of the author', //optional
  49. 'webmaster' => 'email address for person responsible for technical issues' // optional, ignored if atom is used
  50. 'copyright' => 'copyright notice', //optional
  51. 'image' => 'url to image', //optional
  52. 'generator' => 'generator', // optional
  53. 'language' => 'language the feed is written in', // optional
  54. 'ttl' => 'how long in minutes a feed can be cached before refreshing', // optional, ignored if atom is used
  55. 'rating' => 'The PICS rating for the channel.', // optional, ignored if atom is used
  56. 'cloud' => array(
  57. 'domain' => 'domain of the cloud, e.g. rpc.sys.com' // required
  58. 'port' => 'port to connect to' // optional, default to 80
  59. 'path' => 'path of the cloud, e.g. /RPC2' //required
  60. 'registerProcedure' => 'procedure to call, e.g. myCloud.rssPleaseNotify' // required
  61. 'protocol' => 'protocol to use, e.g. soap or xml-rpc' // required
  62. ), // a cloud to be notified of updates // optional, ignored if atom is used
  63. 'textInput' => array(
  64. 'title' => 'the label of the Submit button in the text input area' // required,
  65. 'description' => 'explains the text input area' // required
  66. 'name' => 'the name of the text object in the text input area' // required
  67. 'link' => 'the URL of the CGI script that processes text input requests' // required
  68. ) // a text input box that can be displayed with the feed // optional, ignored if atom is used
  69. 'skipHours' => array(
  70. 'hour in 24 format', // e.g 13 (1pm)
  71. // up to 24 rows whose value is a number between 0 and 23
  72. ) // Hint telling aggregators which hours they can skip // optional, ignored if atom is used
  73. 'skipDays ' => array(
  74. 'a day to skip', // e.g Monday
  75. // up to 7 rows whose value is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or Sunday
  76. ) // Hint telling aggregators which days they can skip // optional, ignored if atom is used
  77. 'itunes' => array(
  78. 'author' => 'Artist column' // optional, default to the main author value
  79. 'owner' => array(
  80. 'name' => 'name of the owner' // optional, default to main author value
  81. 'email' => 'email of the owner' // optional, default to main email value
  82. ) // Owner of the podcast // optional
  83. 'image' => 'album/podcast art' // optional, default to the main image value
  84. 'subtitle' => 'short description' // optional, default to the main description value
  85. 'summary' => 'longer description' // optional, default to the main description value
  86. 'block' => 'Prevent an episode from appearing (yes|no)' // optional
  87. 'category' => array(
  88. array('main' => 'main category', // required
  89. 'sub' => 'sub category' // optional
  90. ),
  91. // up to 3 rows
  92. ) // 'Category column and in iTunes Music Store Browse' // required
  93. 'explicit' => 'parental advisory graphic (yes|no|clean)' // optional
  94. 'keywords' => 'a comma separated list of 12 keywords maximum' // optional
  95. 'new-feed-url' => 'used to inform iTunes of new feed URL location' // optional
  96. ) // Itunes extension data // optional, ignored if atom is used
  97. 'entries' => array(
  98. array(
  99. 'title' => 'title of the feed entry', //required
  100. 'link' => 'url to a feed entry', //required
  101. 'description' => 'short version of a feed entry', // only text, no html, required
  102. 'guid' => 'id of the article, if not given link value will used', //optional
  103. 'content' => 'long version', // can contain html, optional
  104. 'lastUpdate' => 'timestamp of the publication date', // optional
  105. 'comments' => 'comments page of the feed entry', // optional
  106. 'commentRss' => 'the feed url of the associated comments', // optional
  107. 'source' => array(
  108. 'title' => 'title of the original source' // required,
  109. 'url' => 'url of the original source' // required
  110. ) // original source of the feed entry // optional
  111. 'category' => array(
  112. array(
  113. 'term' => 'first category label' // required,
  114. 'scheme' => 'url that identifies a categorization scheme' // optional
  115. ),
  116. array(
  117. //data for the second category and so on
  118. )
  119. ) // list of the attached categories // optional
  120. 'enclosure' => array(
  121. array(
  122. 'url' => 'url of the linked enclosure' // required
  123. 'type' => 'mime type of the enclosure' // optional
  124. 'length' => 'length of the linked content in octets' // optional
  125. ),
  126. array(
  127. //data for the second enclosure and so on
  128. )
  129. ) // list of the enclosures of the feed entry // optional
  130. ),
  131. array(
  132. //data for the second entry and so on
  133. )
  134. )
  135. );]]>
  136. </programlisting>
  137. <para>
  138. References:
  139. <itemizedlist>
  140. <listitem>
  141. <para>
  142. RSS 2.0 规范: <ulink url="http://blogs.law.harvard.edu/tech/rss">RSS 2.0</ulink>
  143. </para>
  144. </listitem>
  145. <listitem>
  146. <para>
  147. Atom 规范:<ulink url="http://tools.ietf.org/html/rfc4287">RFC 4287</ulink>
  148. </para>
  149. </listitem>
  150. <listitem>
  151. <para>
  152. WFW 规范:<ulink url="http://wellformedweb.org/news/wfw_namespace_elements">Well
  153. Formed Web</ulink>
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. iTunes 规范:
  159. <ulink url="http://www.apple.com/itunes/store/podcaststechspecs.html">iTunes Technical
  160. Specifications</ulink>
  161. </para>
  162. </listitem>
  163. </itemizedlist>
  164. </para>
  165. </sect3>
  166. <sect3 id="zend.feed.importing.custom.importbuilder">
  167. <title> 导入定制的数据源 </title>
  168. <para>
  169. 你可以从任何实现 <code>Zend_Feed_Builder_Interface</code> 的数据源创建 Zeed_Feed 实例,只需要实现 <code>getHeader()</code> 和 <code>getEntries()</code> 方法来和 <code>Zend_Feed::importBuilder()</code> 一起使用你的对象。作为一个简单的参考实现,你可以使用 <code>Zend_Feed_Builder</code> 它在构造器里带有一个数组,执行一些校验,然后可以在 <code>importBuilder()</code> 方法中使用。<code>getHeader()</code> 方法必须返回 <code>Zend_Feed_Builder_Header</code> 的实例, <code>getEntries()</code> 必须返回 <code>Zend_Feed_Builder_Entry</code> 实例的数组。
  170. </para>
  171. <note>
  172. <para>
  173. <code>Zend_Feed_Builder</code> 作为具体实现来实现它的用法,(我们)鼓励用户写自己的类来实现 <code>Zend_Feed_Builder_Interface</code>。
  174. </para>
  175. </note>
  176. <para>
  177. <code>Zend_Feed::importBuilder()</code> 用法的例子:
  178. </para>
  179. <programlisting role="php"><![CDATA[<?php
  180. // 从定制的 builder 源导入 feed
  181. $atomFeedFromArray = Zend_Feed::importBuilder(new Zend_Feed_Builder($array));
  182. // 和上面一样,缺省地 Zend_Feed_Atom 实例被返回
  183. $atomFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'atom');
  184. // 从定制的 builder 数组导入 rss feed
  185. $rssFeedFromArray = Zend_Feed::importArray(new Zend_Feed_Builder($array), 'rss');]]>
  186. </programlisting>
  187. </sect3>
  188. <sect3 id="zend.feed.importing.custom.dump">
  189. <title> Dumping feed 内容 </title>
  190. <para>
  191. 为了 dump <code>Zend_Feed_Abstract</code> 实例的内容,使用 <code>send()</code> 或 <code>saveXml()</code> 方法。
  192. </para>
  193. <programlisting role="php"><![CDATA[<?php
  194. assert($feed instanceof Zend_Feed_Abstract);
  195. // dump feed 到标准输出
  196. print $feed->saveXML();
  197. // 发送 http 头和 dump the feed
  198. $feed->send();]]>
  199. </programlisting>
  200. </sect3>
  201. </sect2>
  202. </sect1>
  203. <!--
  204. vim:se ts=4 sw=4 et:
  205. -->