Zend_Feed_Reader.xml 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.reader">
  4. <title>Zend_Feed_Reader</title>
  5. <sect2 id="zend.feed.reader.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Feed_Reader</classname> is a component used to
  9. consume <acronym>RSS</acronym> and Atom feeds of any version, including
  10. <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0,
  11. <acronym>RSS</acronym> 2.0 and Atom 0.3/1.0. The <acronym>API</acronym> for
  12. retrieving feed data is
  13. deliberately simple since <classname>Zend_Feed_Reader</classname> is
  14. capable of searching any feed of any type for the information
  15. requested through the <acronym>API</acronym>. If the typical elements containing this
  16. information are not present, it will adapt and fall back on a
  17. variety of alternative elements instead. This ability to choose from
  18. alternatives removes the need for users to create their own
  19. abstraction layer on top of the component to make it useful or have
  20. any in-depth knowledge of the underlying standards, current
  21. alternatives, and namespaced extensions.
  22. </para>
  23. <para>
  24. Internally, <classname>Zend_Feed_Reader</classname> works almost
  25. entirely on the basis of making XPath queries against the feed <acronym>XML</acronym>'s
  26. Document Object Model. The <acronym>DOM</acronym> is not exposed though a chained
  27. property <acronym>API</acronym> like <classname>Zend_Feed</classname> though the
  28. underlying <classname>DOMDocument</classname>,
  29. <classname>DOMElement</classname> and
  30. <classname>DOMXPath</classname> objects are exposed for external
  31. manipulation. This singular approach to parsing is consistent and
  32. the component offers a plugin system to add to the Feed and Entry
  33. level <acronym>API</acronym> by writing Extensions on a similar basis.
  34. </para>
  35. <para>
  36. Performance is assisted in three ways. First of all,
  37. <classname>Zend_Feed_Reader</classname> supports caching using
  38. <classname>Zend_Cache</classname> to maintain a copy of the original
  39. feed <acronym>XML</acronym>. This allows you to skip network requests for a feed
  40. <acronym>URI</acronym> if
  41. the cache is valid. Second, the Feed and Entry level <acronym>API</acronym> is backed
  42. by an internal cache (non-persistant) so repeat <acronym>API</acronym> calls for the
  43. same feed will avoid additional <acronym>DOM</acronym>/XPath use. Thirdly, importing
  44. feeds from a <acronym>URI</acronym> can take advantage of
  45. <acronym>HTTP</acronym> Conditional GET requests
  46. which allow servers to issue an empty 304 response when the
  47. requested feed has not changed since the last time you requested it.
  48. In the final case, an instance of <classname>Zend_Cache</classname>
  49. will hold the last received feed along with the ETag and
  50. Last-Modified header values sent in the <acronym>HTTP</acronym> response.
  51. </para>
  52. <para>
  53. In relation to <classname>Zend_Feed</classname>,
  54. <classname>Zend_Feed_Reader</classname> was formulated as a free
  55. standing replacement for <classname>Zend_Feed</classname> but it is
  56. not backwards compatible with <classname>Zend_Feed</classname>.
  57. Rather it is an alternative following a different ideology focused
  58. on being simple to use, flexible, consistent and extendable through
  59. the plugin system. <classname>Zend_Feed_Reader</classname> is also
  60. not capable of constructing feeds and delegates this responsibility
  61. to <classname>Zend_Feed_Writer</classname>, its sibling in arms.
  62. </para>
  63. </sect2>
  64. <sect2 id="zend.feed.reader.import">
  65. <title>Importing Feeds</title>
  66. <para>
  67. Importing a feed with <classname>Zend_Feed_Reader</classname> is not
  68. that much different to <classname>Zend_Feed</classname>. Feeds can
  69. be imported from a string, file, <acronym>URI</acronym> or an instance of type
  70. <classname>Zend_Feed_Abstract</classname>. Importing from a <acronym>URI</acronym> can
  71. additionally utilise a <acronym>HTTP</acronym> Conditional GET request. If importing
  72. fails, an exception will be raised. The end result will be an object
  73. of type <classname>Zend_Feed_Reader_FeedInterface</classname>, the
  74. core implementations of which are
  75. <classname>Zend_Feed_Reader_Feed_Rss</classname> and
  76. <classname>Zend_Feed_Reader_Feed_Atom</classname>
  77. (<classname>Zend_Feed</classname> took all the short names!). Both
  78. objects support multiple (all existing) versions of these broad feed
  79. types.
  80. </para>
  81. <para>
  82. In the following example, we import an <acronym>RDF</acronym>/<acronym>RSS</acronym> 1.0
  83. feed and extract some basic information that can be saved to a database or
  84. elsewhere.
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  88. $data = array(
  89. 'title' => $feed->getTitle(),
  90. 'link' => $feed->getLink(),
  91. 'dateModified' => $feed->getDateModified(),
  92. 'description' => $feed->getDescription(),
  93. 'language' => $feed->getLanguage(),
  94. 'entries' => array(),
  95. );
  96. foreach ($feed as $entry) {
  97. $edata = array(
  98. 'title' => $entry->getTitle(),
  99. 'description' => $entry->getDescription(),
  100. 'dateModified' => $entry->getDateModified(),
  101. 'authors' => $entry->getAuthors(),
  102. 'link' => $entry->getLink(),
  103. 'content' => $entry->getContent()
  104. );
  105. $data['entries'][] = $edata;
  106. }
  107. ]]></programlisting>
  108. <para>
  109. The example above demonstrates
  110. <classname>Zend_Feed_Reader</classname>'s <acronym>API</acronym>, and it also
  111. demonstrates some of its internal operation. In reality, the <acronym>RDF</acronym>
  112. feed selected does not have any native date or author elements,
  113. however it does utilise the Dublin Core 1.1 module which offers
  114. namespaced creator and date elements.
  115. <classname>Zend_Feed_Reader</classname> falls back on these and
  116. similar options if no relevant native elements exist. If it
  117. absolutely cannot find an alternative it will return <constant>NULL</constant>,
  118. indicating the information could not be found in the feed. You
  119. should note that classes implementing
  120. <classname>Zend_Feed_Reader_FeedInterface</classname> also implement
  121. the <acronym>SPL</acronym> <classname>Iterator</classname> and
  122. <classname>Countable</classname> interfaces.
  123. </para>
  124. <para>
  125. Feeds can also be imported from strings, files, and even objects of
  126. type <classname>Zend_Feed_Abstract</classname>.
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. // from a URI
  130. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  131. // from a String
  132. $feed = Zend_Feed_Reader::importString($feedXmlString);
  133. // from a file
  134. $feed = Zend_Feed_Reader::importFile('./feed.xml');
  135. // from a Zend_Feed_Abstract object
  136. $zfeed = Zend_Feed::import('http://www.planet-php.net/atom/');
  137. $feed = Zend_Feed_Reader::importFeed($zfeed);
  138. ]]></programlisting>
  139. </sect2>
  140. <sect2 id="zend.feed.reader.sources">
  141. <title>Retrieving Underlying Feed and Entry Sources</title>
  142. <para>
  143. <classname>Zend_Feed_Reader</classname> does its best not to stick
  144. you in a narrow confine. If you need to work on a feed outside of
  145. <classname>Zend_Feed_Reader</classname>, you can extract the base
  146. <classname>DOMDocument</classname> or
  147. <classname>DOMElement</classname> objects from any class, or even an
  148. <acronym>XML</acronym> string containing these. Also provided are methods to extract
  149. the current <classname>DOMXPath</classname> object (with all core
  150. and Extension namespaces registered) and the correct prefix used in
  151. all XPath queries for the current Feed or Entry. The basic methods
  152. to use (on any object) are <methodname>saveXml()</methodname>,
  153. <methodname>getDomDocument()</methodname>,
  154. <methodname>getElement()</methodname>,
  155. <methodname>getXpath()</methodname> and
  156. <methodname>getXpathPrefix()</methodname>. These will let you break
  157. free of <classname>Zend_Feed_Reader</classname> and do whatever else
  158. you want.
  159. </para>
  160. <itemizedlist>
  161. <listitem>
  162. <para>
  163. <methodname>saveXml()</methodname> returns an <acronym>XML</acronym> string
  164. containing only the element representing the current object.
  165. </para>
  166. </listitem>
  167. <listitem>
  168. <para>
  169. <methodname>getDomDocument()</methodname> returns the
  170. <classname>DOMDocument</classname> object representing the
  171. entire feed (even if called from an Entry object).
  172. </para>
  173. </listitem>
  174. <listitem>
  175. <para>
  176. <methodname>getElement()</methodname> returns the
  177. <classname>DOMElement</classname> of the current object
  178. (i.e. the Feed or current Entry).
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <methodname>getXpath()</methodname> returns the
  184. <classname>DOMXPath</classname> object for the current feed
  185. (even if called from an Entry object) with the namespaces of
  186. the current feed type and all loaded Extensions
  187. pre-registered.
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <methodname>getXpathPrefix()</methodname> returns the query
  193. prefix for the current object (i.e. the Feed or current
  194. Entry) which includes the correct XPath query path for that
  195. specific Feed or Entry.
  196. </para>
  197. </listitem>
  198. </itemizedlist>
  199. <para>
  200. Here's an example where a feed might include an <acronym>RSS</acronym> Extension not
  201. supported by <classname>Zend_Feed_Reader</classname> out of the box.
  202. Notably, you could write and register an Extension (covered later)
  203. to do this, but that's not always warranted for a quick check. You
  204. must register any new namespaces on the
  205. <classname>DOMXPath</classname> object before use unless they are
  206. registered by <classname>Zend_Feed_Reader</classname> or an
  207. Extension beforehand.
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  211. $xpathPrefix = $feed->getXpathPrefix();
  212. $xpath = $feed->getXpath();
  213. $xpath->registerNamespace('admin', 'http://webns.net/mvcb/');
  214. $reportErrorsTo = $xpath->evaluate('string('
  215. . $xpathPrefix
  216. . '/admin:errorReportsTo)');
  217. ]]></programlisting>
  218. <warning>
  219. <para>
  220. If you register an already registered namespace with a different
  221. prefix name to that used internally by
  222. <classname>Zend_Feed_Reader</classname>, it will break the
  223. internal operation of this component.
  224. </para>
  225. </warning>
  226. </sect2>
  227. <sect2 id="zend.feed.reader.cache-request">
  228. <title>Cache Support and Intelligent Requests</title>
  229. <sect3 id="zend.feed.reader.cache-request.cache">
  230. <title>Adding Cache Support to Zend_Feed_Reader</title>
  231. <para>
  232. <classname>Zend_Feed_Reader</classname> supports using an
  233. instance of <classname>Zend_Cache</classname> to cache feeds (as
  234. <acronym>XML</acronym>) to avoid unnecessary network requests. Adding a cache is as
  235. simple here as it is for other Zend Framework components, create
  236. and configure your cache and then tell
  237. <classname>Zend_Feed_Reader</classname> to use it! The cache key
  238. used is "<classname>Zend_Feed_Reader_</classname>" followed by the
  239. <acronym>MD5</acronym> hash of the feed's <acronym>URI</acronym>.
  240. </para>
  241. <programlisting language="php"><![CDATA[
  242. $frontendOptions = array(
  243. 'lifetime' => 7200,
  244. 'automatic_serialization' => true
  245. );
  246. $backendOptions = array('cache_dir' => './tmp/');
  247. $cache = Zend_Cache::factory(
  248. 'Core', 'File', $frontendOptions, $backendOptions
  249. );
  250. Zend_Feed_Reader::setCache($cache);
  251. ]]></programlisting>
  252. <note>
  253. <para>
  254. While it's a little off track, you should also consider
  255. adding a cache to
  256. <classname>Zend_Loader_PluginLoader</classname> which is
  257. used by <classname>Zend_Feed_Reader</classname> to load
  258. Extensions.
  259. </para>
  260. </note>
  261. </sect3>
  262. <sect3 id="zend.feed.reader.cache-request.http-conditional-get">
  263. <title>HTTP Conditional GET Support</title>
  264. <para>
  265. The big question often asked when importing a feed frequently, is
  266. if it has even changed. With a cache enabled, you can add <acronym>HTTP</acronym>
  267. Conditional GET support to your arsenal to answer that question.
  268. </para>
  269. <para>
  270. Using this method, you can request feeds from <acronym>URI</acronym>s and include
  271. their last known ETag and Last-Modified response header values
  272. with the request (using the If-None-Match and If-Modified-Since
  273. headers). If the feed on the server remains unchanged, you
  274. should receive a 304 response which tells
  275. <classname>Zend_Feed_Reader</classname> to use the cached
  276. version. If a full feed is sent in a response with a status code
  277. of 200, this means the feed has changed and
  278. <classname>Zend_Feed_Reader</classname> will parse the new
  279. version and save it to the cache. It will also cache the new
  280. ETag and Last-Modified header values for future use.
  281. </para>
  282. <para>
  283. These "conditional" requests are not guaranteed to be supported
  284. by the server you request a <acronym>URI</acronym> of, but can be attempted
  285. regardless. Most common feed sources like blogs should however
  286. have this supported. To enable conditional requests, you will
  287. need to provide a cache to <classname>Zend_Feed_Reader</classname>.
  288. </para>
  289. <programlisting language="php"><![CDATA[
  290. $frontendOptions = array(
  291. 'lifetime' => 86400,
  292. 'automatic_serialization' => true
  293. );
  294. $backendOptions = array('cache_dir' => './tmp/');
  295. $cache = Zend_Cache::factory(
  296. 'Core', 'File', $frontendOptions, $backendOptions
  297. );
  298. Zend_Feed_Reader::setCache($cache);
  299. Zend_Feed_Reader::useHttpConditionalGet();
  300. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  301. ]]></programlisting>
  302. <para>
  303. In the example above, with <acronym>HTTP</acronym> Conditional GET requests enabled,
  304. the response header values for ETag and Last-Modified will be cached
  305. along with the feed. For the next 24hrs (the cache lifetime), feeds will
  306. only be updated on the cache if a non-304 response is received
  307. containing a valid <acronym>RSS</acronym> or Atom <acronym>XML</acronym> document.
  308. </para>
  309. <para>
  310. If you intend on managing request headers from outside
  311. <classname>Zend_Feed_Reader</classname>, you can set the
  312. relevant If-None-Matches and If-Modified-Since request headers
  313. via the <acronym>URI</acronym> import method.
  314. </para>
  315. <programlisting language="php"><![CDATA[
  316. $lastEtagReceived = '5e6cefe7df5a7e95c8b1ba1a2ccaff3d';
  317. $lastModifiedDateReceived = 'Wed, 08 Jul 2009 13:37:22 GMT';
  318. $feed = Zend_Feed_Reader::import(
  319. $uri, $lastEtagReceived, $lastModifiedDateReceived
  320. );
  321. ]]></programlisting>
  322. </sect3>
  323. </sect2>
  324. <sect2 id="zend.feed.reader.locate">
  325. <title>Locating Feed URIs from Websites</title>
  326. <para>
  327. These days, many websites are aware that the location of their <acronym>XML</acronym>
  328. feeds is not always obvious. A small <acronym>RDF</acronym>, <acronym>RSS</acronym> or
  329. Atom graphic helps when the user is reading the page, but what about when a machine
  330. visits trying to identify where your feeds are located? To assist in
  331. this, websites may point to their feeds using &lt;link&gt; tags in
  332. the &lt;head&gt; section of their <acronym>HTML</acronym>. To take advantage of this,
  333. you can use <classname>Zend_Feed_Reader</classname> to locate these
  334. feeds using the static <methodname>findFeedLinks()</methodname>
  335. method.
  336. </para>
  337. <para>
  338. This method calls any <acronym>URI</acronym> and searches for the location of
  339. <acronym>RSS</acronym>, <acronym>RDF</acronym>
  340. and Atom feeds assuming the website's <acronym>HTML</acronym> contains the relevant
  341. links. It then returns a value object where you can check for the existence of a
  342. <acronym>RSS</acronym>, <acronym>RDF</acronym> or Atom feed <acronym>URI</acronym>.
  343. </para>
  344. <para>
  345. The returned object is an <classname>ArrayObject</classname> subclass
  346. called <classname>Zend_Feed_Reader_Collection_FeedLink</classname> so you can cast
  347. it to an array, or iterate over it, to access all the detected links.
  348. However, as a simple shortcut, you can just grab the first RSS, RDF
  349. or Atom link using its public properties as in the example below. Otherwise,
  350. each element of the <classname>ArrayObject</classname> is a simple array
  351. with the keys "type" and "uri" where the type is one of "rdf", "rss" or
  352. "atom".
  353. </para>
  354. <programlisting language="php"><![CDATA[
  355. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  356. if(isset($links->rdf)) {
  357. echo $links->rdf, "\n"; // http://www.planet-php.org/rdf/
  358. }
  359. if(isset($links->rss)) {
  360. echo $links->rss, "\n"; // http://www.planet-php.org/rss/
  361. }
  362. if(isset($links->atom)) {
  363. echo $links->atom, "\n"; // http://www.planet-php.org/atom/
  364. }
  365. ]]></programlisting>
  366. <para>
  367. Based on these links, you can then import from whichever source you
  368. wish in the usual manner.
  369. </para>
  370. <para>
  371. This quick method only gives you one link for each feed type, but
  372. websites may indicate many links of any type. Perhaps it's a news
  373. site with a RSS feed for each news category. You can iterate over
  374. all links using the ArrayObject's iterator.
  375. </para>
  376. <programlisting language="php"><![CDATA[
  377. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  378. foreach ($links as $link) {
  379. echo $link['uri'], "\n";
  380. }
  381. ]]></programlisting>
  382. </sect2>
  383. <sect2 id="zend.feed.reader.attribute-collections">
  384. <title>Attribute Collections</title>
  385. <para>In an attempt to simplify return types, with Zend Framework 1.10 return
  386. types from the various feed and entry level methods may include an object
  387. of type <classname>Zend_Feed_Reader_Collection_CollectionAbstract</classname>.
  388. Despite the special class name which I'll explain below, this is just a simple
  389. subclass of SPL's <classname>ArrayObject</classname>.</para>
  390. <para>The main purpose here is to allow the presentation of as much data as possible
  391. from the requested elements, while still allowing access to the most relevant
  392. data as a simple array. This also enforces a standard approach to returning
  393. such data which previously may have wandered between arrays and objects.</para>
  394. <para>The new class type acts identically to <classname>ArrayObject</classname>
  395. with the sole addition being a new method <methodname>getValues()</methodname>
  396. which returns a simple flat array containing the most relevant information.</para>
  397. <para>A simple example of this is
  398. <methodname>Zend_Feed_Reader_FeedInterface::getCategories()</methodname>.
  399. When used with any RSS or Atom feed, this method will return category data as
  400. a container object called <classname>Zend_Feed_Reader_Collection_Category</classname>.
  401. The container object will contain, per category, three fields of data: term, scheme and
  402. label. The "term" is the basic category name, often machine readable (i.e. plays nice
  403. with URIs). The scheme represents a categorisation scheme (usually a URI identifier) also
  404. known as a "domain" in RSS 2.0. The "label" is a human readable category name which supports
  405. html entities. In RSS 2.0, there is no label attribute so it is always set to the same value as
  406. the term for convenience.</para>
  407. <para>To access category labels by themselves in a simple value array,
  408. you might commit to something like:</para>
  409. <programlisting language="php"><![CDATA[
  410. $feed = Zend_Feed_Reader::import('http://www.example.com/atom.xml');
  411. $categories = $feed->getCategories();
  412. $labels = array();
  413. foreach ($categories as $cat) {
  414. $labels[] = $cat['label']
  415. }
  416. ]]></programlisting>
  417. <para>It's a contrived example, but the point is that the labels are tied up with
  418. other information.</para>
  419. <para>However, the container class allows you to access the "most relevant" data
  420. as a simple array using the <methodname>getValues()</methodname> method. The concept
  421. of "most relevant" is obviously a judgement call. For categories it means the category labels
  422. (not the terms or schemes) while for authors it would be the authors' names
  423. (not their email addresses or URIs). The simple array is flat (just values) and passed
  424. through <methodname>array_unique()</methodname> to remove duplication.</para>
  425. <programlisting language="php"><![CDATA[
  426. $feed = Zend_Feed_Reader::import('http://www.example.com/atom.xml');
  427. $categories = $feed->getCategories();
  428. $labels = $categories->getValues();
  429. ]]></programlisting>
  430. <para>The above example shows how to extract only labels and nothing else thus
  431. giving simple access to the category labels without any additional work to extract
  432. that data by itself.</para>
  433. </sect2>
  434. <sect2 id="zend.feed.reader.retrieve-info">
  435. <title>Retrieving Feed Information</title>
  436. <para>
  437. Retrieving information from a feed (we'll cover entries/items in the
  438. next section though they follow identical principals) uses a clearly
  439. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  440. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  441. sub-versions of these standards and we've tested every single
  442. <acronym>RSS</acronym> and Atom version. While
  443. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  444. tags and elements they present, they nonetheless are all trying to
  445. convey similar information and to reflect this all the differences
  446. and wrangling over alternative tags are handled internally by
  447. <classname>Zend_Feed_Reader</classname> presenting you with an
  448. identical interface for each. Ideally, you should not have to care
  449. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  450. information you want.
  451. </para>
  452. <note><para>
  453. While determining common ground between feed types is itself complex, it
  454. should be noted that RSS in particular is a constantly disputed "specification".
  455. This has its roots in the original RSS 2.0 document which contains ambiguities
  456. and does not detail the correct treatment of all elements. As a result, this
  457. component rigorously applies the RSS 2.0.11 Specification published by the
  458. RSS Advisory Board and its accompanying RSS Best Practices Profile. No
  459. other interpretation of RSS 2.0 will be supported though exceptions may
  460. be allowed where it does not directly prevent the application of the two
  461. documents mentioned above.
  462. </para></note>
  463. <para>
  464. Of course, we don't live in an ideal world so there may be times the
  465. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  466. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  467. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  468. additional data you are trying to extract from feeds. If writing
  469. another Extension is too much trouble, you can simply grab the
  470. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  471. application. Of course, we really do encourage writing an Extension
  472. simply to make it more portable and reusable, and useful Extensions may be proposed
  473. to the Framework for formal addition.
  474. </para>
  475. <para>
  476. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  477. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  478. accounts for a number of included Extensions bundled with
  479. <classname>Zend_Feed_Reader</classname>. The naming of these
  480. Extension sourced methods remain fairly generic - all Extension
  481. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  482. you to retrieve any specific Extension object separately if required.
  483. </para>
  484. <table>
  485. <title>Feed Level API Methods</title>
  486. <tgroup cols="2">
  487. <tbody>
  488. <row>
  489. <entry><methodname>getId()</methodname></entry>
  490. <entry>Returns a unique ID associated with this feed</entry>
  491. </row>
  492. <row>
  493. <entry><methodname>getTitle()</methodname></entry>
  494. <entry>Returns the title of the feed</entry>
  495. </row>
  496. <row>
  497. <entry><methodname>getDescription()</methodname></entry>
  498. <entry>Returns the text description of the feed.</entry>
  499. </row>
  500. <row>
  501. <entry><methodname>getLink()</methodname></entry>
  502. <entry>
  503. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  504. containing the same or
  505. similar information as this feed (i.e. if the feed is from a blog,
  506. it should provide the blog's <acronym>URI</acronym> where the
  507. <acronym>HTML</acronym> version of the entries can be read).
  508. </entry>
  509. </row>
  510. <row>
  511. <entry><methodname>getFeedLink()</methodname></entry>
  512. <entry>
  513. Returns the <acronym>URI</acronym> of this feed, which may be the
  514. same as the <acronym>URI</acronym> used to import the feed. There
  515. are important cases where the feed link may differ because the source
  516. URI is being updated and is intended to be removed in the future.
  517. </entry>
  518. </row>
  519. <row>
  520. <entry><methodname>getAuthors()</methodname></entry>
  521. <entry>
  522. Returns an object of type <classname>Zend_Feed_Reader_Collection_Author</classname>
  523. which is an <classname>ArrayObject</classname> whose elements are each simple
  524. arrays containing any combination of the keys "name", "email" and
  525. "uri". Where irrelevant to the source data, some of these keys may be omitted.
  526. </entry>
  527. </row>
  528. <row>
  529. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  530. <entry>
  531. Returns either the first author known, or with the
  532. optional <varname>$index</varname> parameter any specific
  533. index on the array of Authors as described above (returning
  534. <constant>NULL</constant> if an invalid index).
  535. </entry>
  536. </row>
  537. <row>
  538. <entry><methodname>getDateCreated()</methodname></entry>
  539. <entry>
  540. Returns the date on which this feed was created. Generally
  541. only applicable to Atom where it represents the date the resource
  542. described by an Atom 1.0 document was created. The returned date
  543. will be a <classname>Zend_Date</classname> object.
  544. </entry>
  545. </row>
  546. <row>
  547. <entry><methodname>getDateModified()</methodname></entry>
  548. <entry>
  549. Returns the date on which this feed was last modified. The returned date
  550. will be a <classname>Zend_Date</classname> object.
  551. </entry>
  552. </row>
  553. <row>
  554. <entry><methodname>getLanguage()</methodname></entry>
  555. <entry>
  556. Returns the language of the feed (if defined) or simply the
  557. language noted in the <acronym>XML</acronym> document.
  558. </entry>
  559. </row>
  560. <row>
  561. <entry><methodname>getGenerator()</methodname></entry>
  562. <entry>
  563. Returns the generator of the feed, e.g. the software which
  564. generated it. This may differ between <acronym>RSS</acronym> and Atom
  565. since Atom defines a different notation.
  566. </entry>
  567. </row>
  568. <row>
  569. <entry><methodname>getCopyright()</methodname></entry>
  570. <entry>
  571. Returns any copyright notice associated with the feed.
  572. </entry>
  573. </row>
  574. <row>
  575. <entry><methodname>getHubs()</methodname></entry>
  576. <entry>
  577. Returns an array of all Hub Server <acronym>URI</acronym> endpoints which
  578. are advertised by the feed for use with the Pubsubhubbub
  579. Protocol, allowing subscriptions to the feed for real-time updates.
  580. </entry>
  581. </row>
  582. <row>
  583. <entry><methodname>getCategories()</methodname></entry>
  584. <entry>
  585. Returns a <classname>Zend_Feed_Reader_Collection_Category</classname>
  586. object containing the details of any categories associated with the
  587. overall feed. The supported fields include "term" (the machine readable
  588. category name), "scheme" (the categorisation scheme/domain for this
  589. category), and "label" (a html decoded human readable category name).
  590. Where any of the three fields are absent from the field, they are either
  591. set to the closest available alternative or, in the case of "scheme",
  592. set to <constant>NULL</constant>.
  593. </entry>
  594. </row>
  595. </tbody>
  596. </tgroup>
  597. </table>
  598. <para>
  599. Given the variety of feeds in the wild, some of these methods will
  600. undoubtedly return <constant>NULL</constant> indicating the relevant information
  601. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  602. will fall back on alternative elements during its search. For
  603. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  604. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  605. <code>&lt;lastBuildDate&gt;</code> tag and/or a
  606. <code>&lt;pubDate&gt;</code> element. But what if it doesn't, maybe
  607. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  608. <code>&lt;atom:updated&gt;</code> element with identical information
  609. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  610. could simply look at the entries, pick the most recent, and use its
  611. <code>&lt;pubDate&gt;</code> element. Assuming it exists... Many
  612. feeds also use Dublin Core 1.0/1.1 <code>&lt;dc:date&gt;</code>
  613. elements for feeds/entries. Or we could find Atom lurking again.
  614. </para>
  615. <para>
  616. The point is, <classname>Zend_Feed_Reader</classname> was designed
  617. to know this. When you ask for the modification date (or anything
  618. else), it will run off and search for all these alternatives until
  619. it either gives up and returns <constant>NULL</constant>, or finds an
  620. alternative that should have the right answer.
  621. </para>
  622. <para>
  623. In addition to the above methods, all Feed objects implement methods
  624. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  625. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  626. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  627. </para>
  628. <table>
  629. <title>Extended Feed Level API Methods</title>
  630. <tgroup cols="2">
  631. <tbody>
  632. <row>
  633. <entry><methodname>getDomDocument()</methodname></entry>
  634. <entry>
  635. Returns the parent
  636. <classname>DOMDocument</classname> object for the
  637. entire source <acronym>XML</acronym> document
  638. </entry>
  639. </row>
  640. <row>
  641. <entry><methodname>getElement()</methodname></entry>
  642. <entry>
  643. Returns the current feed level
  644. <classname>DOMElement</classname> object
  645. </entry>
  646. </row>
  647. <row>
  648. <entry><methodname>saveXml()</methodname></entry>
  649. <entry>
  650. Returns a string containing an <acronym>XML</acronym> document of the
  651. entire feed element (this is not the original
  652. document but a rebuilt version)
  653. </entry>
  654. </row>
  655. <row>
  656. <entry><methodname>getXpath()</methodname></entry>
  657. <entry>
  658. Returns the <classname>DOMXPath</classname> object
  659. used internally to run queries on the
  660. <classname>DOMDocument</classname> object (this
  661. includes core and Extension namespaces
  662. pre-registered)
  663. </entry>
  664. </row>
  665. <row>
  666. <entry><methodname>getXpathPrefix()</methodname></entry>
  667. <entry>
  668. Returns the valid <acronym>DOM</acronym> path prefix prepended
  669. to all XPath queries matching the feed being queried
  670. </entry>
  671. </row>
  672. <row>
  673. <entry><methodname>getEncoding()</methodname></entry>
  674. <entry>
  675. Returns the encoding of the source <acronym>XML</acronym> document
  676. (note: this cannot account for errors such as the
  677. server sending documents in a different encoding). Where not
  678. defined, the default UTF-8 encoding of Unicode is applied.
  679. </entry>
  680. </row>
  681. <row>
  682. <entry><methodname>count()</methodname></entry>
  683. <entry>
  684. Returns a count of the entries or items this feed contains
  685. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  686. interface)
  687. </entry>
  688. </row>
  689. <row>
  690. <entry><methodname>current()</methodname></entry>
  691. <entry>
  692. Returns either the current entry (using the current index
  693. from <methodname>key()</methodname>)
  694. </entry>
  695. </row>
  696. <row>
  697. <entry><methodname>key()</methodname></entry>
  698. <entry>Returns the current entry index</entry>
  699. </row>
  700. <row>
  701. <entry><methodname>next()</methodname></entry>
  702. <entry>Increments the entry index value by one</entry>
  703. </row>
  704. <row>
  705. <entry><methodname>rewind()</methodname></entry>
  706. <entry>Resets the entry index to 0</entry>
  707. </row>
  708. <row>
  709. <entry><methodname>valid()</methodname></entry>
  710. <entry>
  711. Checks that the current entry index is valid, i.e.
  712. it does fall below 0 and does not exceed the number
  713. of entries existing.
  714. </entry>
  715. </row>
  716. <row>
  717. <entry><methodname>getExtensions()</methodname></entry>
  718. <entry>
  719. Returns an array of all Extension objects loaded for
  720. the current feed (note: both feed-level and entry-level Extensions
  721. exist, and only feed-level Extensions are returned here).
  722. The array keys are of the form {ExtensionName}_Feed.
  723. </entry>
  724. </row>
  725. <row>
  726. <entry><methodname>getExtension(string $name)</methodname></entry>
  727. <entry>
  728. Returns an Extension object for the feed registered under the
  729. provided name. This allows more fine-grained access to
  730. Extensions which may otherwise be hidden within the implementation
  731. of the standard <acronym>API</acronym> methods.
  732. </entry>
  733. </row>
  734. <row>
  735. <entry><methodname>getType()</methodname></entry>
  736. <entry>
  737. Returns a static class constant (e.g.
  738. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  739. i.e. Atom 0.3) indicating exactly what kind of feed
  740. is being consumed.
  741. </entry>
  742. </row>
  743. </tbody>
  744. </tgroup>
  745. </table>
  746. </sect2>
  747. <sect2 id="zend.feed.reader.entry">
  748. <title>Retrieving Entry/Item Information</title>
  749. <para>
  750. Retrieving information for specific entries or items (depending on
  751. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  752. Accessing entries is simply a matter of iterating over a Feed object
  753. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  754. objects implement and calling the appropriate method on each.
  755. </para>
  756. <table>
  757. <title>Entry Level API Methods</title>
  758. <tgroup cols="2">
  759. <tbody>
  760. <row>
  761. <entry><methodname>getId()</methodname></entry>
  762. <entry>Returns a unique ID for the current entry.</entry>
  763. </row>
  764. <row>
  765. <entry><methodname>getTitle()</methodname></entry>
  766. <entry>Returns the title of the current entry.</entry>
  767. </row>
  768. <row>
  769. <entry><methodname>getDescription()</methodname></entry>
  770. <entry>Returns a description of the current entry.</entry>
  771. </row>
  772. <row>
  773. <entry><methodname>getLink()</methodname></entry>
  774. <entry>
  775. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  776. of the current entry.
  777. </entry>
  778. </row>
  779. <row>
  780. <entry><methodname>getPermaLink()</methodname></entry>
  781. <entry>
  782. Returns the permanent link to the current entry. In most cases,
  783. this is the same as using <methodname>getLink()</methodname>.
  784. </entry>
  785. </row>
  786. <row>
  787. <entry><methodname>getAuthors()</methodname></entry>
  788. <entry>
  789. Returns an object of type <classname>Zend_Feed_Reader_Collection_Author</classname>
  790. which is an <classname>ArrayObject</classname> whose elements are each simple
  791. arrays containing any combination of the keys "name", "email" and
  792. "uri". Where irrelevant to the source data, some of these keys may be omitted.
  793. </entry>
  794. </row>
  795. <row>
  796. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  797. <entry>
  798. Returns either the first author known, or with the
  799. optional <varname>$index</varname> parameter any specific
  800. index on the array of Authors as described above (returning
  801. <constant>NULL</constant> if an invalid index).
  802. </entry>
  803. </row>
  804. <row>
  805. <entry><methodname>getDateCreated()</methodname></entry>
  806. <entry>
  807. Returns the date on which the current entry was
  808. created. Generally only applicable to Atom where it
  809. represents the date the resource described by an
  810. Atom 1.0 document was created.
  811. </entry>
  812. </row>
  813. <row>
  814. <entry><methodname>getDateModified()</methodname></entry>
  815. <entry>
  816. Returns the date on which the current entry was last
  817. modified
  818. </entry>
  819. </row>
  820. <row>
  821. <entry><methodname>getContent()</methodname></entry>
  822. <entry>
  823. Returns the content of the current entry (this has any
  824. entities reversed if possible assuming the content type is
  825. <acronym>HTML</acronym>). The description is returned if a
  826. separate content element does not exist.
  827. </entry>
  828. </row>
  829. <row>
  830. <entry><methodname>getEnclosure()</methodname></entry>
  831. <entry>
  832. Returns an array containing the value of all
  833. attributes from a multi-media &lt;enclosure&gt; element including
  834. as array keys: <emphasis>url</emphasis>,
  835. <emphasis>length</emphasis>, <emphasis>type</emphasis>.
  836. In accordance with the RSS Best Practices Profile of the RSS
  837. Advisory Board, no support is offers for multiple enclosures
  838. since such support forms no part of the RSS specification.
  839. </entry>
  840. </row>
  841. <row>
  842. <entry><methodname>getCommentCount()</methodname></entry>
  843. <entry>
  844. Returns the number of comments made on this entry at the
  845. time the feed was last generated
  846. </entry>
  847. </row>
  848. <row>
  849. <entry><methodname>getCommentLink()</methodname></entry>
  850. <entry>
  851. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  852. page where comments can be made on this entry
  853. </entry>
  854. </row>
  855. <row>
  856. <entry>
  857. <methodname>getCommentFeedLink([string $type =
  858. 'atom'|'rss'])</methodname>
  859. </entry>
  860. <entry>
  861. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  862. containing all comments for this entry (type defaults to
  863. Atom/<acronym>RSS</acronym> depending on current feed type).
  864. </entry>
  865. </row>
  866. <row>
  867. <entry><methodname>getCategories()</methodname></entry>
  868. <entry>
  869. Returns a <classname>Zend_Feed_Reader_Collection_Category</classname>
  870. object containing the details of any categories associated with the
  871. entry. The supported fields include "term" (the machine readable
  872. category name), "scheme" (the categorisation scheme/domain for this
  873. category), and "label" (a html decoded human readable category name).
  874. Where any of the three fields are absent from the field, they are either
  875. set to the closest available alternative or, in the case of "scheme",
  876. set to <constant>NULL</constant>.
  877. </entry>
  878. </row>
  879. </tbody>
  880. </tgroup>
  881. </table>
  882. <para>
  883. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  884. exception of the Iterator methods which are not needed here.
  885. </para>
  886. <caution>
  887. <para>
  888. There is often confusion over the concepts of modified and
  889. created dates. In Atom, these are two clearly defined concepts
  890. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  891. <acronym>RSS</acronym> 2.0
  892. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  893. which typically refers to the date this entry was published,
  894. i.e. a creation date of sorts. This is not always the case, and
  895. it may change with updates or not. As a result, if you really
  896. want to check whether an entry has changed, don't rely on the
  897. results of <methodname>getDateModified()</methodname>. Instead,
  898. consider tracking the <acronym>MD5</acronym> hash of three other elements
  899. concatenated, e.g. using <methodname>getTitle()</methodname>,
  900. <methodname>getDescription()</methodname> and
  901. <methodname>getContent()</methodname>. If the entry was truly
  902. updated, this hash computation will give a different result than
  903. previously saved hashes for the same entry. This is obviously
  904. content oriented, and will not assist in detecting changes to other
  905. relevant elements. Atom feeds should not require such steps.
  906. </para>
  907. <para>
  908. Further muddying the
  909. waters, dates in feeds may follow different standards. Atom and
  910. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  911. and <acronym>RSS</acronym> dates should
  912. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  913. which is also common. Date methods
  914. will throw an exception if <classname>Zend_Date</classname>
  915. cannot load the date string using one of the above standards, or
  916. the PHP recognised possibilities for <acronym>RSS</acronym> dates.
  917. </para>
  918. </caution>
  919. <warning>
  920. <para>
  921. The values returned from these methods are not validated. This
  922. means users must perform validation on all retrieved data
  923. including the filtering of any <acronym>HTML</acronym> such as from
  924. <methodname>getContent()</methodname> before it is output from
  925. your application. Remember that most feeds come from external
  926. sources, and therefore the default assumption should be that
  927. they cannot be trusted.
  928. </para>
  929. </warning>
  930. <table>
  931. <title>Extended Entry Level API Methods</title>
  932. <tgroup cols="2">
  933. <tbody>
  934. <row>
  935. <entry><methodname>getDomDocument()</methodname></entry>
  936. <entry>
  937. Returns the parent
  938. <classname>DOMDocument</classname> object for the
  939. entire feed (not just the current entry)
  940. </entry>
  941. </row>
  942. <row>
  943. <entry><methodname>getElement()</methodname></entry>
  944. <entry>
  945. Returns the current entry level
  946. <classname>DOMElement</classname> object
  947. </entry>
  948. </row>
  949. <row>
  950. <entry><methodname>getXpath()</methodname></entry>
  951. <entry>
  952. Returns the <classname>DOMXPath</classname> object
  953. used internally to run queries on the
  954. <classname>DOMDocument</classname> object (this
  955. includes core and Extension namespaces
  956. pre-registered)
  957. </entry>
  958. </row>
  959. <row>
  960. <entry><methodname>getXpathPrefix()</methodname></entry>
  961. <entry>
  962. Returns the valid <acronym>DOM</acronym> path prefix prepended
  963. to all XPath queries matching the entry being queried
  964. </entry>
  965. </row>
  966. <row>
  967. <entry><methodname>getEncoding()</methodname></entry>
  968. <entry>
  969. Returns the encoding of the source <acronym>XML</acronym> document
  970. (note: this cannot account for errors such as the server sending
  971. documents in a different encoding). The default encoding applied
  972. in the absence of any other is the UTF-8 encoding of Unicode.
  973. </entry>
  974. </row>
  975. <row>
  976. <entry><methodname>getExtensions()</methodname></entry>
  977. <entry>
  978. Returns an array of all Extension objects loaded for
  979. the current entry (note: both feed-level and entry-level
  980. Extensions exist, and only entry-level Extensions are returned
  981. here). The array keys are in the form {ExtensionName}_Entry.
  982. </entry>
  983. </row>
  984. <row>
  985. <entry><methodname>getExtension(string $name)</methodname></entry>
  986. <entry>
  987. Returns an Extension object for the entry registered under the
  988. provided name. This allows more fine-grained access to
  989. Extensions which may otherwise be hidden within the implementation
  990. of the standard <acronym>API</acronym> methods.
  991. </entry>
  992. </row>
  993. <row>
  994. <entry><methodname>getType()</methodname></entry>
  995. <entry>
  996. Returns a static class constant (e.g.
  997. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  998. i.e. Atom 0.3) indicating exactly what kind
  999. of feed is being consumed.
  1000. </entry>
  1001. </row>
  1002. </tbody>
  1003. </tgroup>
  1004. </table>
  1005. </sect2>
  1006. <sect2 id="zend.feed.reader.extending">
  1007. <title>Extending Feed and Entry APIs</title>
  1008. <para>
  1009. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  1010. methods at both the feed and entry level which cover the retrieval
  1011. of information not already supported by
  1012. <classname>Zend_Feed_Reader</classname>. Given the number of
  1013. <acronym>RSS</acronym> and
  1014. Atom extensions that exist, this is a good thing since
  1015. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  1016. everything.
  1017. </para>
  1018. <para>
  1019. There are two types of Extensions possible, those which retrieve
  1020. information from elements which are immediate children of the root
  1021. element (e.g. <code>&lt;channel&gt;</code> for <acronym>RSS</acronym> or
  1022. <code>&lt;feed&gt;</code> for Atom) and those who retrieve
  1023. information from child elements of an entry (e.g.
  1024. <code>&lt;item&gt;</code> for <acronym>RSS</acronym> or <code>&lt;entry&gt;</code> for
  1025. Atom). On the filesystem these are grouped as classes within
  1026. a namespace based on the extension standard's name. For
  1027. example, internally we have
  1028. <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  1029. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  1030. classes which are two Extensions implementing Dublin Core
  1031. 1.0/1.1 support.
  1032. </para>
  1033. <para>
  1034. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  1035. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  1036. will be familiar from other Zend Framework components.
  1037. <classname>Zend_Feed_Reader</classname> already bundles a number of
  1038. these Extensions, however those which are not used internally and
  1039. registered by default (so called Core Extensions) must be registered
  1040. to <classname>Zend_Feed_Reader</classname> before they are used. The
  1041. bundled Extensions include:
  1042. </para>
  1043. <table>
  1044. <title>Core Extensions (pre-registered)</title>
  1045. <tgroup cols="2">
  1046. <tbody>
  1047. <row>
  1048. <entry>DublinCore (Feed and Entry)</entry>
  1049. <entry>Implements support for Dublin Core Metadata Element Set 1.0
  1050. and 1.1 </entry>
  1051. </row>
  1052. <row>
  1053. <entry>Content (Entry only)</entry>
  1054. <entry>Implements support for Content 1.0</entry>
  1055. </row>
  1056. <row>
  1057. <entry>Atom (Feed and Entry)</entry>
  1058. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  1059. </row>
  1060. <row>
  1061. <entry>Slash</entry>
  1062. <entry>
  1063. Implements support for the Slash <acronym>RSS</acronym> 1.0 module
  1064. </entry>
  1065. </row>
  1066. <row>
  1067. <entry>WellFormedWeb</entry>
  1068. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  1069. </row>
  1070. <row>
  1071. <entry>Thread</entry>
  1072. <entry>Implements support for Atom Threading Extensions as described
  1073. in <acronym>RFC</acronym> 4685</entry>
  1074. </row>
  1075. <row>
  1076. <entry>Podcast</entry>
  1077. <entry>
  1078. Implements support for the Podcast 1.0 <constant>DTD</constant> from
  1079. Apple
  1080. </entry>
  1081. </row>
  1082. </tbody>
  1083. </tgroup>
  1084. </table>
  1085. <para>
  1086. The Core Extensions are somewhat special since they are extremely
  1087. common and multi-faceted. For example, we have a Core Extension for Atom.
  1088. Atom is implemented as an Extension (not just a base class) because it
  1089. doubles as a valid <acronym>RSS</acronym> module - you can insert
  1090. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  1091. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  1092. common Extensions like Dublin Core.
  1093. </para>
  1094. <table>
  1095. <title>Non-Core Extensions (must register manually)</title>
  1096. <tgroup cols="2">
  1097. <tbody>
  1098. <row>
  1099. <entry>Syndication</entry>
  1100. <entry>
  1101. Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds
  1102. </entry>
  1103. </row>
  1104. <row>
  1105. <entry>CreativeCommons</entry>
  1106. <entry>
  1107. A <acronym>RSS</acronym> module that adds an element at the
  1108. &lt;channel&gt; or &lt;item&gt; level that specifies which Creative
  1109. Commons license applies.
  1110. </entry>
  1111. </row>
  1112. </tbody>
  1113. </tgroup>
  1114. </table>
  1115. <para>
  1116. The additional non-Core Extensions are offered but not registered to
  1117. <classname>Zend_Feed_Reader</classname> by default. If you want to
  1118. use them, you'll need to tell
  1119. <classname>Zend_Feed_Reader</classname> to load them in advance of
  1120. importing a feed. Additional non-Core Extensions will be included
  1121. in future iterations of the component.
  1122. </para>
  1123. <para>
  1124. Registering an Extension with
  1125. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  1126. is available to Feed and Entry objects, is a simple affair using the
  1127. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  1128. the optional Slash Extension, and discover that it can be directly
  1129. called from the Entry level <acronym>API</acronym> without any effort. Note that
  1130. Extension names are case sensitive and use camel casing for multiple
  1131. terms.
  1132. </para>
  1133. <programlisting language="php"><![CDATA[
  1134. Zend_Feed_Reader::registerExtension('Syndication');
  1135. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1136. $updatePeriod = $feed->current()->getUpdatePeriod();
  1137. ]]></programlisting>
  1138. <para>
  1139. In the simple example above, we checked how frequently a feed is being updated
  1140. using the <methodname>getUpdatePeriod()</methodname>
  1141. method. Since it's not part of
  1142. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  1143. a method supported by the newly registered Syndication Extension.
  1144. </para>
  1145. <para>
  1146. As you can also notice, the new methods from Extensions are accessible from the main
  1147. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  1148. you can also directly access any Extension object for a similar result as seen below.
  1149. </para>
  1150. <programlisting language="php"><![CDATA[
  1151. Zend_Feed_Reader::registerExtension('Syndication');
  1152. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1153. $syndication = $feed->getExtension('Syndication');
  1154. $updatePeriod = $syndication->getUpdatePeriod();
  1155. ]]></programlisting>
  1156. <sect3 id="zend.feed.reader.extending.feed">
  1157. <title>Writing Zend_Feed_Reader Extensions</title>
  1158. <para>
  1159. Inevitably, there will be times when the
  1160. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  1161. of getting something you need from a feed or entry. You can use
  1162. the underlying source objects, like
  1163. <classname>DOMDocument</classname>, to get these by hand however
  1164. there is a more reusable method available by writing Extensions
  1165. supporting these new queries.
  1166. </para>
  1167. <para>
  1168. As an example, let's take the case of a purely fictitious
  1169. corporation named Jungle Books. Jungle Books have been
  1170. publishing a lot of reviews on books they sell (from external
  1171. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1172. feed. Their marketing department realises that web applications
  1173. using this feed cannot currently figure out exactly what book is
  1174. being reviewed. To make life easier for everyone, they determine
  1175. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1176. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1177. <acronym>ISBN</acronym>-13 number of
  1178. the publication the entry concerns. They define the new
  1179. <code>&lt;isbn&gt;</code> element quite simply with a standard
  1180. name and namespace <acronym>URI</acronym>:
  1181. </para>
  1182. <programlisting language="php"><![CDATA[
  1183. JungleBooks 1.0:
  1184. http://example.com/junglebooks/rss/module/1.0/
  1185. ]]></programlisting>
  1186. <para>
  1187. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1188. something similar to:
  1189. </para>
  1190. <programlisting language="php"><![CDATA[
  1191. <?xml version="1.0" encoding="utf-8" ?>
  1192. <rss version="2.0"
  1193. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1194. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1195. <channel>
  1196. <title>Jungle Books Customer Reviews</title>
  1197. <link>http://example.com/junglebooks</link>
  1198. <description>Many book reviews!</description>
  1199. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1200. <jungle:dayPopular>
  1201. http://example.com/junglebooks/book/938
  1202. </jungle:dayPopular>
  1203. <item>
  1204. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1205. <link>http://example.com/junglebooks/review/987</link>
  1206. <author>Confused Physics Student</author>
  1207. <content:encoded>
  1208. A romantic square?!
  1209. </content:encoded>
  1210. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1211. <jungle:isbn>048627263X</jungle:isbn>
  1212. </item>
  1213. </channel>
  1214. </rss>
  1215. ]]></programlisting>
  1216. <para>
  1217. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1218. extension would require the following class (using your own class
  1219. namespace outside of Zend).
  1220. </para>
  1221. <programlisting language="php"><![CDATA[
  1222. class My_FeedReader_Extension_JungleBooks_Entry
  1223. extends Zend_Feed_Reader_Extension_EntryAbstract
  1224. {
  1225. public function getIsbn()
  1226. {
  1227. if (isset($this->_data['isbn'])) {
  1228. return $this->_data['isbn'];
  1229. }
  1230. $isbn = $this->_xpath->evaluate(
  1231. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1232. );
  1233. if (!$isbn) {
  1234. $isbn = null;
  1235. }
  1236. $this->_data['isbn'] = $isbn;
  1237. return $this->_data['isbn'];
  1238. }
  1239. protected function _registerNamespaces()
  1240. {
  1241. $this->_xpath->registerNamespace(
  1242. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1243. );
  1244. }
  1245. }
  1246. ]]></programlisting>
  1247. <para>
  1248. This extension is easy enough to follow. It creates a new method
  1249. <methodname>getIsbn()</methodname> which runs an XPath query on
  1250. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1251. <code>&lt;jungle:isbn&gt;</code> element. It can optionally
  1252. store this to the internal non-persistent cache (no need to keep
  1253. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1254. value is returned to the caller. At the end we have a protected
  1255. method (it's abstract so it must exist) which registers the
  1256. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1257. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1258. element being used in Atom feeds - and all Extensions which use
  1259. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1260. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1261. extra code.
  1262. </para>
  1263. <para>
  1264. Since this Extension is stored outside of Zend Framework, you'll
  1265. need to register the path prefix for your Extensions so
  1266. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1267. After that, it's merely a matter of registering the Extension,
  1268. if it's not already loaded, and using it in practice.
  1269. </para>
  1270. <programlisting language="php"><![CDATA[
  1271. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1272. Zend_Feed_Reader::addPrefixPath(
  1273. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1274. );
  1275. Zend_Feed_Reader::registerExtension('JungleBooks');
  1276. }
  1277. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1278. // ISBN for whatever book the first entry in the feed was concerned with
  1279. $firstIsbn = $feed->current()->getIsbn();
  1280. ]]></programlisting>
  1281. <para>
  1282. Writing a feed level Extension is not much different. The
  1283. example feed from earlier included an unmentioned
  1284. <code>&lt;jungle:dayPopular&gt;</code> element which Jungle
  1285. Books have added to their standard to include a link to the
  1286. day's most popular book (in terms of visitor traffic). Here's
  1287. an Extension which adds a
  1288. <methodname>getDaysPopularBookLink()</methodname> method to the
  1289. feel level <acronym>API</acronym>.
  1290. </para>
  1291. <programlisting language="php"><![CDATA[
  1292. class My_FeedReader_Extension_JungleBooks_Feed
  1293. extends Zend_Feed_Reader_Extension_FeedAbstract
  1294. {
  1295. public function getDaysPopularBookLink()
  1296. {
  1297. if (isset($this->_data['dayPopular'])) {
  1298. return $this->_data['dayPopular'];
  1299. }
  1300. $dayPopular = $this->_xpath->evaluate(
  1301. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1302. );
  1303. if (!$dayPopular) {
  1304. $dayPopular = null;
  1305. }
  1306. $this->_data['dayPopular'] = $dayPopular;
  1307. return $this->_data['dayPopular'];
  1308. }
  1309. protected function _registerNamespaces()
  1310. {
  1311. $this->_xpath->registerNamespace(
  1312. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1313. );
  1314. }
  1315. }
  1316. ]]></programlisting>
  1317. <para>
  1318. Let's repeat the last example using a custom Extension to show the
  1319. method being used.
  1320. </para>
  1321. <programlisting language="php"><![CDATA[
  1322. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1323. Zend_Feed_Reader::addPrefixPath(
  1324. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1325. );
  1326. Zend_Feed_Reader::registerExtension('JungleBooks');
  1327. }
  1328. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1329. // URI to the information page of the day's most popular book with visitors
  1330. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1331. // ISBN for whatever book the first entry in the feed was concerned with
  1332. $firstIsbn = $feed->current()->getIsbn();
  1333. ]]></programlisting>
  1334. <para>
  1335. Going through these examples, you'll note that we don't register
  1336. feed and entry Extensions separately. Extensions within the same
  1337. standard may or may not include both a feed and entry class, so
  1338. <classname>Zend_Feed_Reader</classname> only requires you to
  1339. register the overall parent name, e.g. JungleBooks, DublinCore,
  1340. Slash. Internally, it can check at what level Extensions exist
  1341. and load them up if found. In our case, we have a full set of
  1342. Extensions now: <classname>JungleBooks_Feed</classname> and
  1343. <classname>JungleBooks_Entry</classname>.
  1344. </para>
  1345. </sect3>
  1346. </sect2>
  1347. </sect1>