Zend_Feed_Reader.xml 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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>
  386. In an attempt to simplify return types, with Zend Framework 1.10 return
  387. types from the various feed and entry level methods may include an object
  388. of type <classname>Zend_Feed_Reader_Collection_CollectionAbstract</classname>.
  389. Despite the special class name which I'll explain below, this is just a simple
  390. subclass of SPL's <classname>ArrayObject</classname>.
  391. </para>
  392. <para>
  393. The main purpose here is to allow the presentation of as much data as possible
  394. from the requested elements, while still allowing access to the most relevant
  395. data as a simple array. This also enforces a standard approach to returning
  396. such data which previously may have wandered between arrays and objects.
  397. </para>
  398. <para>
  399. The new class type acts identically to <classname>ArrayObject</classname>
  400. with the sole addition being a new method <methodname>getValues()</methodname>
  401. which returns a simple flat array containing the most relevant information.
  402. </para>
  403. <para>
  404. A simple example of this is
  405. <methodname>Zend_Feed_Reader_FeedInterface::getCategories()</methodname>. When used with
  406. any RSS or Atom feed, this method will return category data as a container object called
  407. <classname>Zend_Feed_Reader_Collection_Category</classname>. The container object will
  408. contain, per category, three fields of data: term, scheme and label. The "term" is the
  409. basic category name, often machine readable (i.e. plays nice with URIs). The scheme
  410. represents a categorisation scheme (usually a URI identifier) also known as a "domain"
  411. in RSS 2.0. The "label" is a human readable category name which supports
  412. <acronym>HTML</acronym> entities. In RSS 2.0, there is no label attribute so it is
  413. always set to the same value as the term for convenience.
  414. </para>
  415. <para>
  416. To access category labels by themselves in a simple value array,
  417. you might commit to something like:
  418. </para>
  419. <programlisting language="php"><![CDATA[
  420. $feed = Zend_Feed_Reader::import('http://www.example.com/atom.xml');
  421. $categories = $feed->getCategories();
  422. $labels = array();
  423. foreach ($categories as $cat) {
  424. $labels[] = $cat['label']
  425. }
  426. ]]></programlisting>
  427. <para>
  428. It's a contrived example, but the point is that the labels are tied up with
  429. other information.
  430. </para>
  431. <para>
  432. However, the container class allows you to access the "most relevant" data
  433. as a simple array using the <methodname>getValues()</methodname> method. The concept
  434. of "most relevant" is obviously a judgement call. For categories it means the category
  435. labels (not the terms or schemes) while for authors it would be the authors' names
  436. (not their email addresses or URIs). The simple array is flat (just values) and passed
  437. through <methodname>array_unique()</methodname> to remove duplication.
  438. </para>
  439. <programlisting language="php"><![CDATA[
  440. $feed = Zend_Feed_Reader::import('http://www.example.com/atom.xml');
  441. $categories = $feed->getCategories();
  442. $labels = $categories->getValues();
  443. ]]></programlisting>
  444. <para>
  445. The above example shows how to extract only labels and nothing else thus
  446. giving simple access to the category labels without any additional work to extract
  447. that data by itself.
  448. </para>
  449. </sect2>
  450. <sect2 id="zend.feed.reader.retrieve-info">
  451. <title>Retrieving Feed Information</title>
  452. <para>
  453. Retrieving information from a feed (we'll cover entries/items in the
  454. next section though they follow identical principals) uses a clearly
  455. defined <acronym>API</acronym> which is exactly the same regardless of whether the feed
  456. in question is <acronym>RSS</acronym>/<acronym>RDF</acronym>/Atom. The same goes for
  457. sub-versions of these standards and we've tested every single
  458. <acronym>RSS</acronym> and Atom version. While
  459. the underlying feed <acronym>XML</acronym> can differ substantially in terms of the
  460. tags and elements they present, they nonetheless are all trying to
  461. convey similar information and to reflect this all the differences
  462. and wrangling over alternative tags are handled internally by
  463. <classname>Zend_Feed_Reader</classname> presenting you with an
  464. identical interface for each. Ideally, you should not have to care
  465. whether a feed is <acronym>RSS</acronym> or Atom so long as you can extract the
  466. information you want.
  467. </para>
  468. <note>
  469. <para>
  470. While determining common ground between feed types is itself complex, it
  471. should be noted that RSS in particular is a constantly disputed "specification".
  472. This has its roots in the original RSS 2.0 document which contains ambiguities
  473. and does not detail the correct treatment of all elements. As a result, this
  474. component rigorously applies the RSS 2.0.11 Specification published by the
  475. RSS Advisory Board and its accompanying RSS Best Practices Profile. No
  476. other interpretation of RSS 2.0 will be supported though exceptions may
  477. be allowed where it does not directly prevent the application of the two
  478. documents mentioned above.
  479. </para>
  480. </note>
  481. <para>
  482. Of course, we don't live in an ideal world so there may be times the
  483. <acronym>API</acronym> just does not cover what you're looking for. To assist you,
  484. <classname>Zend_Feed_Reader</classname> offers a plugin system which
  485. allows you to write Extensions to expand the core <acronym>API</acronym> and cover any
  486. additional data you are trying to extract from feeds. If writing
  487. another Extension is too much trouble, you can simply grab the
  488. underlying <acronym>DOM</acronym> or XPath objects and do it by hand in your
  489. application. Of course, we really do encourage writing an Extension
  490. simply to make it more portable and reusable, and useful Extensions may be proposed
  491. to the Framework for formal addition.
  492. </para>
  493. <para>
  494. Here's a summary of the Core <acronym>API</acronym> for Feeds. You should note it
  495. comprises not only the basic <acronym>RSS</acronym> and Atom standards, but also
  496. accounts for a number of included Extensions bundled with
  497. <classname>Zend_Feed_Reader</classname>. The naming of these
  498. Extension sourced methods remain fairly generic - all Extension
  499. methods operate at the same level as the Core <acronym>API</acronym> though we do allow
  500. you to retrieve any specific Extension object separately if required.
  501. </para>
  502. <table>
  503. <title>Feed Level API Methods</title>
  504. <tgroup cols="2">
  505. <tbody>
  506. <row>
  507. <entry><methodname>getId()</methodname></entry>
  508. <entry>Returns a unique ID associated with this feed</entry>
  509. </row>
  510. <row>
  511. <entry><methodname>getTitle()</methodname></entry>
  512. <entry>Returns the title of the feed</entry>
  513. </row>
  514. <row>
  515. <entry><methodname>getDescription()</methodname></entry>
  516. <entry>Returns the text description of the feed.</entry>
  517. </row>
  518. <row>
  519. <entry><methodname>getLink()</methodname></entry>
  520. <entry>
  521. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> website
  522. containing the same or
  523. similar information as this feed (i.e. if the feed is from a blog,
  524. it should provide the blog's <acronym>URI</acronym> where the
  525. <acronym>HTML</acronym> version of the entries can be read).
  526. </entry>
  527. </row>
  528. <row>
  529. <entry><methodname>getFeedLink()</methodname></entry>
  530. <entry>
  531. Returns the <acronym>URI</acronym> of this feed, which may be the
  532. same as the <acronym>URI</acronym> used to import the feed. There
  533. are important cases where the feed link may differ because the source
  534. URI is being updated and is intended to be removed in the future.
  535. </entry>
  536. </row>
  537. <row>
  538. <entry><methodname>getAuthors()</methodname></entry>
  539. <entry>
  540. Returns an object of type
  541. <classname>Zend_Feed_Reader_Collection_Author</classname> which is an
  542. <classname>ArrayObject</classname> whose elements are each simple arrays
  543. containing any combination of the keys "name", "email" and "uri". Where
  544. irrelevant to the source data, some of these keys may be omitted.
  545. </entry>
  546. </row>
  547. <row>
  548. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  549. <entry>
  550. Returns either the first author known, or with the
  551. optional <varname>$index</varname> parameter any specific
  552. index on the array of Authors as described above (returning
  553. <constant>NULL</constant> if an invalid index).
  554. </entry>
  555. </row>
  556. <row>
  557. <entry><methodname>getDateCreated()</methodname></entry>
  558. <entry>
  559. Returns the date on which this feed was created. Generally
  560. only applicable to Atom where it represents the date the resource
  561. described by an Atom 1.0 document was created. The returned date
  562. will be a <classname>Zend_Date</classname> object.
  563. </entry>
  564. </row>
  565. <row>
  566. <entry><methodname>getDateModified()</methodname></entry>
  567. <entry>
  568. Returns the date on which this feed was last modified. The returned date
  569. will be a <classname>Zend_Date</classname> object.
  570. </entry>
  571. </row>
  572. <row>
  573. <entry><methodname>getLastBuildDate()</methodname></entry>
  574. <entry>
  575. Returns the date on which this feed was last built. The returned date
  576. will be a <classname>Zend_Date</classname> object. This is only
  577. supported by RSS - Atom feeds will always return NULL.
  578. </entry>
  579. </row>
  580. <row>
  581. <entry><methodname>getLanguage()</methodname></entry>
  582. <entry>
  583. Returns the language of the feed (if defined) or simply the
  584. language noted in the <acronym>XML</acronym> document.
  585. </entry>
  586. </row>
  587. <row>
  588. <entry><methodname>getGenerator()</methodname></entry>
  589. <entry>
  590. Returns the generator of the feed, e.g. the software which
  591. generated it. This may differ between <acronym>RSS</acronym> and Atom
  592. since Atom defines a different notation.
  593. </entry>
  594. </row>
  595. <row>
  596. <entry><methodname>getCopyright()</methodname></entry>
  597. <entry>Returns any copyright notice associated with the feed.</entry>
  598. </row>
  599. <row>
  600. <entry><methodname>getHubs()</methodname></entry>
  601. <entry>
  602. Returns an array of all Hub Server <acronym>URI</acronym> endpoints
  603. which are advertised by the feed for use with the Pubsubhubbub
  604. Protocol, allowing subscriptions to the feed for real-time updates.
  605. </entry>
  606. </row>
  607. <row>
  608. <entry><methodname>getCategories()</methodname></entry>
  609. <entry>
  610. Returns a <classname>Zend_Feed_Reader_Collection_Category</classname>
  611. object containing the details of any categories associated with the
  612. overall feed. The supported fields include "term" (the machine readable
  613. category name), "scheme" (the categorisation scheme/domain for this
  614. category), and "label" (a <acronym>HTML</acronym> decoded human readable
  615. category name). Where any of the three fields are absent from the field,
  616. they are either set to the closest available alternative or, in the case
  617. of "scheme", set to <constant>NULL</constant>.
  618. </entry>
  619. </row>
  620. <row>
  621. <entry><methodname>getImage()</methodname></entry>
  622. <entry>
  623. Returns an array containing data relating to any feed image or logo,
  624. or NULL if no image found. The resulting array may contain the following
  625. keys: uri, link, title, description, height, and width. Atom logos only
  626. contain a URI so the remaining metadata is drawn from RSS feeds only.
  627. </entry>
  628. </row>
  629. </tbody>
  630. </tgroup>
  631. </table>
  632. <para>
  633. Given the variety of feeds in the wild, some of these methods will
  634. undoubtedly return <constant>NULL</constant> indicating the relevant information
  635. couldn't be located. Where possible, <classname>Zend_Feed_Reader</classname>
  636. will fall back on alternative elements during its search. For
  637. example, searching an <acronym>RSS</acronym> feed for a modification date is more
  638. complicated than it looks. <acronym>RSS</acronym> 2.0 feeds should include a
  639. <command>&lt;lastBuildDate&gt;</command> tag and (or) a
  640. <command>&lt;pubDate&gt;</command> element. But what if it doesn't, maybe
  641. this is an <acronym>RSS</acronym> 1.0 feed? Perhaps it instead has an
  642. <command>&lt;atom:updated&gt;</command> element with identical information
  643. (Atom may be used to supplement <acronym>RSS</acronym>'s syntax)? Failing that, we
  644. could simply look at the entries, pick the most recent, and use its
  645. <command>&lt;pubDate&gt;</command> element. Assuming it exists... Many
  646. feeds also use Dublin Core 1.0/1.1 <command>&lt;dc:date&gt;</command>
  647. elements for feeds and entries. Or we could find Atom lurking again.
  648. </para>
  649. <para>
  650. The point is, <classname>Zend_Feed_Reader</classname> was designed
  651. to know this. When you ask for the modification date (or anything
  652. else), it will run off and search for all these alternatives until
  653. it either gives up and returns <constant>NULL</constant>, or finds an
  654. alternative that should have the right answer.
  655. </para>
  656. <para>
  657. In addition to the above methods, all Feed objects implement methods
  658. for retrieving the <acronym>DOM</acronym> and XPath objects for the current feeds as
  659. described earlier. Feed objects also implement the <acronym>SPL</acronym> Iterator and
  660. Countable interfaces. The extended <acronym>API</acronym> is summarised below.
  661. </para>
  662. <table>
  663. <title>Extended Feed Level API Methods</title>
  664. <tgroup cols="2">
  665. <tbody>
  666. <row>
  667. <entry><methodname>getDomDocument()</methodname></entry>
  668. <entry>
  669. Returns the parent
  670. <classname>DOMDocument</classname> object for the
  671. entire source <acronym>XML</acronym> document
  672. </entry>
  673. </row>
  674. <row>
  675. <entry><methodname>getElement()</methodname></entry>
  676. <entry>
  677. Returns the current feed level
  678. <classname>DOMElement</classname> object
  679. </entry>
  680. </row>
  681. <row>
  682. <entry><methodname>saveXml()</methodname></entry>
  683. <entry>
  684. Returns a string containing an <acronym>XML</acronym> document of the
  685. entire feed element (this is not the original
  686. document but a rebuilt version)
  687. </entry>
  688. </row>
  689. <row>
  690. <entry><methodname>getXpath()</methodname></entry>
  691. <entry>
  692. Returns the <classname>DOMXPath</classname> object
  693. used internally to run queries on the
  694. <classname>DOMDocument</classname> object (this
  695. includes core and Extension namespaces
  696. pre-registered)
  697. </entry>
  698. </row>
  699. <row>
  700. <entry><methodname>getXpathPrefix()</methodname></entry>
  701. <entry>
  702. Returns the valid <acronym>DOM</acronym> path prefix prepended
  703. to all XPath queries matching the feed being queried
  704. </entry>
  705. </row>
  706. <row>
  707. <entry><methodname>getEncoding()</methodname></entry>
  708. <entry>
  709. Returns the encoding of the source <acronym>XML</acronym> document
  710. (note: this cannot account for errors such as the
  711. server sending documents in a different encoding). Where not
  712. defined, the default UTF-8 encoding of Unicode is applied.
  713. </entry>
  714. </row>
  715. <row>
  716. <entry><methodname>count()</methodname></entry>
  717. <entry>
  718. Returns a count of the entries or items this feed contains
  719. (implements <acronym>SPL</acronym> <classname>Countable</classname>
  720. interface)
  721. </entry>
  722. </row>
  723. <row>
  724. <entry><methodname>current()</methodname></entry>
  725. <entry>
  726. Returns either the current entry (using the current index
  727. from <methodname>key()</methodname>)
  728. </entry>
  729. </row>
  730. <row>
  731. <entry><methodname>key()</methodname></entry>
  732. <entry>Returns the current entry index</entry>
  733. </row>
  734. <row>
  735. <entry><methodname>next()</methodname></entry>
  736. <entry>Increments the entry index value by one</entry>
  737. </row>
  738. <row>
  739. <entry><methodname>rewind()</methodname></entry>
  740. <entry>Resets the entry index to 0</entry>
  741. </row>
  742. <row>
  743. <entry><methodname>valid()</methodname></entry>
  744. <entry>
  745. Checks that the current entry index is valid, i.e.
  746. it does fall below 0 and does not exceed the number
  747. of entries existing.
  748. </entry>
  749. </row>
  750. <row>
  751. <entry><methodname>getExtensions()</methodname></entry>
  752. <entry>
  753. Returns an array of all Extension objects loaded for
  754. the current feed (note: both feed-level and entry-level Extensions
  755. exist, and only feed-level Extensions are returned here).
  756. The array keys are of the form {ExtensionName}_Feed.
  757. </entry>
  758. </row>
  759. <row>
  760. <entry><methodname>getExtension(string $name)</methodname></entry>
  761. <entry>
  762. Returns an Extension object for the feed registered under the
  763. provided name. This allows more fine-grained access to
  764. Extensions which may otherwise be hidden within the implementation
  765. of the standard <acronym>API</acronym> methods.
  766. </entry>
  767. </row>
  768. <row>
  769. <entry><methodname>getType()</methodname></entry>
  770. <entry>
  771. Returns a static class constant (e.g.
  772. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  773. i.e. Atom 0.3) indicating exactly what kind of feed
  774. is being consumed.
  775. </entry>
  776. </row>
  777. </tbody>
  778. </tgroup>
  779. </table>
  780. </sect2>
  781. <sect2 id="zend.feed.reader.entry">
  782. <title>Retrieving Entry/Item Information</title>
  783. <para>
  784. Retrieving information for specific entries or items (depending on
  785. whether you speak Atom or <acronym>RSS</acronym>) is identical to feed level data.
  786. Accessing entries is simply a matter of iterating over a Feed object
  787. or using the <acronym>SPL</acronym> <classname>Iterator</classname> interface Feed
  788. objects implement and calling the appropriate method on each.
  789. </para>
  790. <table>
  791. <title>Entry Level API Methods</title>
  792. <tgroup cols="2">
  793. <tbody>
  794. <row>
  795. <entry><methodname>getId()</methodname></entry>
  796. <entry>Returns a unique ID for the current entry.</entry>
  797. </row>
  798. <row>
  799. <entry><methodname>getTitle()</methodname></entry>
  800. <entry>Returns the title of the current entry.</entry>
  801. </row>
  802. <row>
  803. <entry><methodname>getDescription()</methodname></entry>
  804. <entry>Returns a description of the current entry.</entry>
  805. </row>
  806. <row>
  807. <entry><methodname>getLink()</methodname></entry>
  808. <entry>
  809. Returns a <acronym>URI</acronym> to the <acronym>HTML</acronym> version
  810. of the current entry.
  811. </entry>
  812. </row>
  813. <row>
  814. <entry><methodname>getPermaLink()</methodname></entry>
  815. <entry>
  816. Returns the permanent link to the current entry. In most cases,
  817. this is the same as using <methodname>getLink()</methodname>.
  818. </entry>
  819. </row>
  820. <row>
  821. <entry><methodname>getAuthors()</methodname></entry>
  822. <entry>
  823. Returns an object of type
  824. <classname>Zend_Feed_Reader_Collection_Author</classname> which is an
  825. <classname>ArrayObject</classname> whose elements are each simple arrays
  826. containing any combination of the keys "name", "email" and "uri". Where
  827. irrelevant to the source data, some of these keys may be omitted.
  828. </entry>
  829. </row>
  830. <row>
  831. <entry><methodname>getAuthor(integer $index = 0)</methodname></entry>
  832. <entry>
  833. Returns either the first author known, or with the
  834. optional <varname>$index</varname> parameter any specific
  835. index on the array of Authors as described above (returning
  836. <constant>NULL</constant> if an invalid index).
  837. </entry>
  838. </row>
  839. <row>
  840. <entry><methodname>getDateCreated()</methodname></entry>
  841. <entry>
  842. Returns the date on which the current entry was
  843. created. Generally only applicable to Atom where it
  844. represents the date the resource described by an
  845. Atom 1.0 document was created.
  846. </entry>
  847. </row>
  848. <row>
  849. <entry><methodname>getDateModified()</methodname></entry>
  850. <entry>
  851. Returns the date on which the current entry was last
  852. modified
  853. </entry>
  854. </row>
  855. <row>
  856. <entry><methodname>getContent()</methodname></entry>
  857. <entry>
  858. Returns the content of the current entry (this has any
  859. entities reversed if possible assuming the content type is
  860. <acronym>HTML</acronym>). The description is returned if a
  861. separate content element does not exist.
  862. </entry>
  863. </row>
  864. <row>
  865. <entry><methodname>getEnclosure()</methodname></entry>
  866. <entry>
  867. Returns an array containing the value of all
  868. attributes from a multi-media &lt;enclosure&gt; element including
  869. as array keys: <emphasis>url</emphasis>,
  870. <emphasis>length</emphasis>, <emphasis>type</emphasis>.
  871. In accordance with the RSS Best Practices Profile of the RSS
  872. Advisory Board, no support is offers for multiple enclosures
  873. since such support forms no part of the RSS specification.
  874. </entry>
  875. </row>
  876. <row>
  877. <entry><methodname>getCommentCount()</methodname></entry>
  878. <entry>
  879. Returns the number of comments made on this entry at the
  880. time the feed was last generated
  881. </entry>
  882. </row>
  883. <row>
  884. <entry><methodname>getCommentLink()</methodname></entry>
  885. <entry>
  886. Returns a <acronym>URI</acronym> pointing to the <acronym>HTML</acronym>
  887. page where comments can be made on this entry
  888. </entry>
  889. </row>
  890. <row>
  891. <entry>
  892. <methodname>getCommentFeedLink([string $type =
  893. 'atom'|'rss'])</methodname>
  894. </entry>
  895. <entry>
  896. Returns a <acronym>URI</acronym> pointing to a feed of the provided type
  897. containing all comments for this entry (type defaults to
  898. Atom/<acronym>RSS</acronym> depending on current feed type).
  899. </entry>
  900. </row>
  901. <row>
  902. <entry><methodname>getCategories()</methodname></entry>
  903. <entry>
  904. Returns a <classname>Zend_Feed_Reader_Collection_Category</classname>
  905. object containing the details of any categories associated with the
  906. entry. The supported fields include "term" (the machine readable
  907. category name), "scheme" (the categorisation scheme/domain for this
  908. category), and "label" (a <acronym>HTML</acronym> decoded human readable
  909. category name). Where any of the three fields are absent from the field,
  910. they are either set to the closest available alternative or, in the case
  911. of "scheme", set to <constant>NULL</constant>.
  912. </entry>
  913. </row>
  914. </tbody>
  915. </tgroup>
  916. </table>
  917. <para>
  918. The extended <acronym>API</acronym> for entries is identical to that for feeds with the
  919. exception of the Iterator methods which are not needed here.
  920. </para>
  921. <caution>
  922. <para>
  923. There is often confusion over the concepts of modified and
  924. created dates. In Atom, these are two clearly defined concepts
  925. (so knock yourself out) but in <acronym>RSS</acronym> they are vague.
  926. <acronym>RSS</acronym> 2.0
  927. defines a single <emphasis>&lt;pubDate&gt;</emphasis> element
  928. which typically refers to the date this entry was published,
  929. i.e. a creation date of sorts. This is not always the case, and
  930. it may change with updates or not. As a result, if you really
  931. want to check whether an entry has changed, don't rely on the
  932. results of <methodname>getDateModified()</methodname>. Instead,
  933. consider tracking the <acronym>MD5</acronym> hash of three other elements
  934. concatenated, e.g. using <methodname>getTitle()</methodname>,
  935. <methodname>getDescription()</methodname> and
  936. <methodname>getContent()</methodname>. If the entry was truly
  937. updated, this hash computation will give a different result than
  938. previously saved hashes for the same entry. This is obviously
  939. content oriented, and will not assist in detecting changes to other
  940. relevant elements. Atom feeds should not require such steps.
  941. </para>
  942. <para>
  943. Further muddying the
  944. waters, dates in feeds may follow different standards. Atom and
  945. Dublin Core dates should follow <acronym>ISO</acronym> 8601,
  946. and <acronym>RSS</acronym> dates should
  947. follow <acronym>RFC</acronym> 822 or <acronym>RFC</acronym> 2822
  948. which is also common. Date methods
  949. will throw an exception if <classname>Zend_Date</classname>
  950. cannot load the date string using one of the above standards, or the
  951. <acronym>PHP</acronym> recognised possibilities for <acronym>RSS</acronym> dates.
  952. </para>
  953. </caution>
  954. <warning>
  955. <para>
  956. The values returned from these methods are not validated. This
  957. means users must perform validation on all retrieved data
  958. including the filtering of any <acronym>HTML</acronym> such as from
  959. <methodname>getContent()</methodname> before it is output from
  960. your application. Remember that most feeds come from external
  961. sources, and therefore the default assumption should be that
  962. they cannot be trusted.
  963. </para>
  964. </warning>
  965. <table>
  966. <title>Extended Entry Level API Methods</title>
  967. <tgroup cols="2">
  968. <tbody>
  969. <row>
  970. <entry><methodname>getDomDocument()</methodname></entry>
  971. <entry>
  972. Returns the parent
  973. <classname>DOMDocument</classname> object for the
  974. entire feed (not just the current entry)
  975. </entry>
  976. </row>
  977. <row>
  978. <entry><methodname>getElement()</methodname></entry>
  979. <entry>
  980. Returns the current entry level
  981. <classname>DOMElement</classname> object
  982. </entry>
  983. </row>
  984. <row>
  985. <entry><methodname>getXpath()</methodname></entry>
  986. <entry>
  987. Returns the <classname>DOMXPath</classname> object
  988. used internally to run queries on the
  989. <classname>DOMDocument</classname> object (this
  990. includes core and Extension namespaces
  991. pre-registered)
  992. </entry>
  993. </row>
  994. <row>
  995. <entry><methodname>getXpathPrefix()</methodname></entry>
  996. <entry>
  997. Returns the valid <acronym>DOM</acronym> path prefix prepended
  998. to all XPath queries matching the entry being queried
  999. </entry>
  1000. </row>
  1001. <row>
  1002. <entry><methodname>getEncoding()</methodname></entry>
  1003. <entry>
  1004. Returns the encoding of the source <acronym>XML</acronym> document
  1005. (note: this cannot account for errors such as the server sending
  1006. documents in a different encoding). The default encoding applied
  1007. in the absence of any other is the UTF-8 encoding of Unicode.
  1008. </entry>
  1009. </row>
  1010. <row>
  1011. <entry><methodname>getExtensions()</methodname></entry>
  1012. <entry>
  1013. Returns an array of all Extension objects loaded for
  1014. the current entry (note: both feed-level and entry-level
  1015. Extensions exist, and only entry-level Extensions are returned
  1016. here). The array keys are in the form {ExtensionName}_Entry.
  1017. </entry>
  1018. </row>
  1019. <row>
  1020. <entry><methodname>getExtension(string $name)</methodname></entry>
  1021. <entry>
  1022. Returns an Extension object for the entry registered under the
  1023. provided name. This allows more fine-grained access to
  1024. Extensions which may otherwise be hidden within the implementation
  1025. of the standard <acronym>API</acronym> methods.
  1026. </entry>
  1027. </row>
  1028. <row>
  1029. <entry><methodname>getType()</methodname></entry>
  1030. <entry>
  1031. Returns a static class constant (e.g.
  1032. <constant>Zend_Feed_Reader::TYPE_ATOM_03</constant>,
  1033. i.e. Atom 0.3) indicating exactly what kind
  1034. of feed is being consumed.
  1035. </entry>
  1036. </row>
  1037. </tbody>
  1038. </tgroup>
  1039. </table>
  1040. </sect2>
  1041. <sect2 id="zend.feed.reader.extending">
  1042. <title>Extending Feed and Entry APIs</title>
  1043. <para>
  1044. Extending <classname>Zend_Feed_Reader</classname> allows you to add
  1045. methods at both the feed and entry level which cover the retrieval
  1046. of information not already supported by
  1047. <classname>Zend_Feed_Reader</classname>. Given the number of
  1048. <acronym>RSS</acronym> and
  1049. Atom extensions that exist, this is a good thing since
  1050. <classname>Zend_Feed_Reader</classname> couldn't possibly add
  1051. everything.
  1052. </para>
  1053. <para>
  1054. There are two types of Extensions possible, those which retrieve
  1055. information from elements which are immediate children of the root
  1056. element (e.g. <command>&lt;channel&gt;</command> for <acronym>RSS</acronym> or
  1057. <command>&lt;feed&gt;</command> for Atom) and those who retrieve
  1058. information from child elements of an entry (e.g.
  1059. <command>&lt;item&gt;</command> for <acronym>RSS</acronym> or
  1060. <command>&lt;entry&gt;</command> for Atom). On the filesystem these are grouped as
  1061. classes within a namespace based on the extension standard's name. For example,
  1062. internally we have <classname>Zend_Feed_Reader_Extension_DublinCore_Feed</classname>
  1063. and <classname>Zend_Feed_Reader_Extension_DublinCore_Entry</classname>
  1064. classes which are two Extensions implementing Dublin Core
  1065. 1.0 and 1.1 support.
  1066. </para>
  1067. <para>
  1068. Extensions are loaded into <classname>Zend_Feed_Reader</classname>
  1069. using <classname>Zend_Loader_PluginLoader</classname>, so their operation
  1070. will be familiar from other Zend Framework components.
  1071. <classname>Zend_Feed_Reader</classname> already bundles a number of
  1072. these Extensions, however those which are not used internally and
  1073. registered by default (so called Core Extensions) must be registered
  1074. to <classname>Zend_Feed_Reader</classname> before they are used. The
  1075. bundled Extensions include:
  1076. </para>
  1077. <table>
  1078. <title>Core Extensions (pre-registered)</title>
  1079. <tgroup cols="2">
  1080. <tbody>
  1081. <row>
  1082. <entry>DublinCore (Feed and Entry)</entry>
  1083. <entry>
  1084. Implements support for Dublin Core Metadata Element Set 1.0 and 1.1
  1085. </entry>
  1086. </row>
  1087. <row>
  1088. <entry>Content (Entry only)</entry>
  1089. <entry>Implements support for Content 1.0</entry>
  1090. </row>
  1091. <row>
  1092. <entry>Atom (Feed and Entry)</entry>
  1093. <entry>Implements support for Atom 0.3 and Atom 1.0</entry>
  1094. </row>
  1095. <row>
  1096. <entry>Slash</entry>
  1097. <entry>
  1098. Implements support for the Slash <acronym>RSS</acronym> 1.0 module
  1099. </entry>
  1100. </row>
  1101. <row>
  1102. <entry>WellFormedWeb</entry>
  1103. <entry>Implements support for the Well Formed Web CommentAPI 1.0</entry>
  1104. </row>
  1105. <row>
  1106. <entry>Thread</entry>
  1107. <entry>
  1108. Implements support for Atom Threading Extensions as described
  1109. in <acronym>RFC</acronym> 4685
  1110. </entry>
  1111. </row>
  1112. <row>
  1113. <entry>Podcast</entry>
  1114. <entry>
  1115. Implements support for the Podcast 1.0 <constant>DTD</constant> from
  1116. Apple
  1117. </entry>
  1118. </row>
  1119. </tbody>
  1120. </tgroup>
  1121. </table>
  1122. <para>
  1123. The Core Extensions are somewhat special since they are extremely
  1124. common and multi-faceted. For example, we have a Core Extension for Atom.
  1125. Atom is implemented as an Extension (not just a base class) because it
  1126. doubles as a valid <acronym>RSS</acronym> module - you can insert
  1127. Atom elements into <acronym>RSS</acronym> feeds. I've even seen
  1128. <acronym>RDF</acronym> feeds which use a lot of Atom in place of more
  1129. common Extensions like Dublin Core.
  1130. </para>
  1131. <table>
  1132. <title>Non-Core Extensions (must register manually)</title>
  1133. <tgroup cols="2">
  1134. <tbody>
  1135. <row>
  1136. <entry>Syndication</entry>
  1137. <entry>
  1138. Implements Syndication 1.0 support for <acronym>RSS</acronym> feeds
  1139. </entry>
  1140. </row>
  1141. <row>
  1142. <entry>CreativeCommons</entry>
  1143. <entry>
  1144. A <acronym>RSS</acronym> module that adds an element at the
  1145. &lt;channel&gt; or &lt;item&gt; level that specifies which Creative
  1146. Commons license applies.
  1147. </entry>
  1148. </row>
  1149. </tbody>
  1150. </tgroup>
  1151. </table>
  1152. <para>
  1153. The additional non-Core Extensions are offered but not registered to
  1154. <classname>Zend_Feed_Reader</classname> by default. If you want to
  1155. use them, you'll need to tell
  1156. <classname>Zend_Feed_Reader</classname> to load them in advance of
  1157. importing a feed. Additional non-Core Extensions will be included
  1158. in future iterations of the component.
  1159. </para>
  1160. <para>
  1161. Registering an Extension with
  1162. <classname>Zend_Feed_Reader</classname>, so it is loaded and its <acronym>API</acronym>
  1163. is available to Feed and Entry objects, is a simple affair using the
  1164. <classname>Zend_Loader_PluginLoader</classname>. Here we register
  1165. the optional Slash Extension, and discover that it can be directly
  1166. called from the Entry level <acronym>API</acronym> without any effort. Note that
  1167. Extension names are case sensitive and use camel casing for multiple
  1168. terms.
  1169. </para>
  1170. <programlisting language="php"><![CDATA[
  1171. Zend_Feed_Reader::registerExtension('Syndication');
  1172. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1173. $updatePeriod = $feed->current()->getUpdatePeriod();
  1174. ]]></programlisting>
  1175. <para>
  1176. In the simple example above, we checked how frequently a feed is being updated
  1177. using the <methodname>getUpdatePeriod()</methodname>
  1178. method. Since it's not part of
  1179. <classname>Zend_Feed_Reader</classname>'s core <acronym>API</acronym>, it could only be
  1180. a method supported by the newly registered Syndication Extension.
  1181. </para>
  1182. <para>
  1183. As you can also notice, the new methods from Extensions are accessible from the main
  1184. <acronym>API</acronym> using <acronym>PHP</acronym>'s magic methods. As an alternative,
  1185. you can also directly access any Extension object for a similar result as seen below.
  1186. </para>
  1187. <programlisting language="php"><![CDATA[
  1188. Zend_Feed_Reader::registerExtension('Syndication');
  1189. $feed = Zend_Feed_Reader::import('http://rss.slashdot.org/Slashdot/slashdot');
  1190. $syndication = $feed->getExtension('Syndication');
  1191. $updatePeriod = $syndication->getUpdatePeriod();
  1192. ]]></programlisting>
  1193. <sect3 id="zend.feed.reader.extending.feed">
  1194. <title>Writing Zend_Feed_Reader Extensions</title>
  1195. <para>
  1196. Inevitably, there will be times when the
  1197. <classname>Zend_Feed_Reader</classname> <acronym>API</acronym> is just not capable
  1198. of getting something you need from a feed or entry. You can use
  1199. the underlying source objects, like
  1200. <classname>DOMDocument</classname>, to get these by hand however
  1201. there is a more reusable method available by writing Extensions
  1202. supporting these new queries.
  1203. </para>
  1204. <para>
  1205. As an example, let's take the case of a purely fictitious
  1206. corporation named Jungle Books. Jungle Books have been
  1207. publishing a lot of reviews on books they sell (from external
  1208. sources and customers), which are distributed as an <acronym>RSS</acronym> 2.0
  1209. feed. Their marketing department realises that web applications
  1210. using this feed cannot currently figure out exactly what book is
  1211. being reviewed. To make life easier for everyone, they determine
  1212. that the geek department needs to extend <acronym>RSS</acronym> 2.0 to include a
  1213. new element per entry supplying the <acronym>ISBN</acronym>-10 or
  1214. <acronym>ISBN</acronym>-13 number of
  1215. the publication the entry concerns. They define the new
  1216. <command>&lt;isbn&gt;</command> element quite simply with a standard
  1217. name and namespace <acronym>URI</acronym>:
  1218. </para>
  1219. <programlisting language="php"><![CDATA[
  1220. JungleBooks 1.0:
  1221. http://example.com/junglebooks/rss/module/1.0/
  1222. ]]></programlisting>
  1223. <para>
  1224. A snippet of <acronym>RSS</acronym> containing this extension in practice could be
  1225. something similar to:
  1226. </para>
  1227. <programlisting language="php"><![CDATA[
  1228. <?xml version="1.0" encoding="utf-8" ?>
  1229. <rss version="2.0"
  1230. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  1231. xmlns:jungle="http://example.com/junglebooks/rss/module/1.0/">
  1232. <channel>
  1233. <title>Jungle Books Customer Reviews</title>
  1234. <link>http://example.com/junglebooks</link>
  1235. <description>Many book reviews!</description>
  1236. <pubDate>Fri, 26 Jun 2009 19:15:10 GMT</pubDate>
  1237. <jungle:dayPopular>
  1238. http://example.com/junglebooks/book/938
  1239. </jungle:dayPopular>
  1240. <item>
  1241. <title>Review Of Flatland: A Romance of Many Dimensions</title>
  1242. <link>http://example.com/junglebooks/review/987</link>
  1243. <author>Confused Physics Student</author>
  1244. <content:encoded>
  1245. A romantic square?!
  1246. </content:encoded>
  1247. <pubDate>Thu, 25 Jun 2009 20:03:28 -0700</pubDate>
  1248. <jungle:isbn>048627263X</jungle:isbn>
  1249. </item>
  1250. </channel>
  1251. </rss>
  1252. ]]></programlisting>
  1253. <para>
  1254. Implementing this new <acronym>ISBN</acronym> element as a simple entry level
  1255. extension would require the following class (using your own class
  1256. namespace outside of Zend).
  1257. </para>
  1258. <programlisting language="php"><![CDATA[
  1259. class My_FeedReader_Extension_JungleBooks_Entry
  1260. extends Zend_Feed_Reader_Extension_EntryAbstract
  1261. {
  1262. public function getIsbn()
  1263. {
  1264. if (isset($this->_data['isbn'])) {
  1265. return $this->_data['isbn'];
  1266. }
  1267. $isbn = $this->_xpath->evaluate(
  1268. 'string(' . $this->getXpathPrefix() . '/jungle:isbn)'
  1269. );
  1270. if (!$isbn) {
  1271. $isbn = null;
  1272. }
  1273. $this->_data['isbn'] = $isbn;
  1274. return $this->_data['isbn'];
  1275. }
  1276. protected function _registerNamespaces()
  1277. {
  1278. $this->_xpath->registerNamespace(
  1279. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1280. );
  1281. }
  1282. }
  1283. ]]></programlisting>
  1284. <para>
  1285. This extension is easy enough to follow. It creates a new method
  1286. <methodname>getIsbn()</methodname> which runs an XPath query on
  1287. the current entry to extract the <acronym>ISBN</acronym> number enclosed by the
  1288. <command>&lt;jungle:isbn&gt;</command> element. It can optionally
  1289. store this to the internal non-persistent cache (no need to keep
  1290. querying the <acronym>DOM</acronym> if it's called again on the same entry). The
  1291. value is returned to the caller. At the end we have a protected
  1292. method (it's abstract so it must exist) which registers the
  1293. Jungle Books namespace for their custom <acronym>RSS</acronym> module. While we
  1294. call this an <acronym>RSS</acronym> module, there's nothing to prevent the same
  1295. element being used in Atom feeds - and all Extensions which use
  1296. the prefix provided by <methodname>getXpathPrefix()</methodname>
  1297. are actually neutral and work on <acronym>RSS</acronym> or Atom feeds with no
  1298. extra code.
  1299. </para>
  1300. <para>
  1301. Since this Extension is stored outside of Zend Framework, you'll
  1302. need to register the path prefix for your Extensions so
  1303. <classname>Zend_Loader_PluginLoader</classname> can find them.
  1304. After that, it's merely a matter of registering the Extension,
  1305. if it's not already loaded, and using it in practice.
  1306. </para>
  1307. <programlisting language="php"><![CDATA[
  1308. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1309. Zend_Feed_Reader::addPrefixPath(
  1310. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1311. );
  1312. Zend_Feed_Reader::registerExtension('JungleBooks');
  1313. }
  1314. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1315. // ISBN for whatever book the first entry in the feed was concerned with
  1316. $firstIsbn = $feed->current()->getIsbn();
  1317. ]]></programlisting>
  1318. <para>
  1319. Writing a feed level Extension is not much different. The
  1320. example feed from earlier included an unmentioned
  1321. <command>&lt;jungle:dayPopular&gt;</command> element which Jungle
  1322. Books have added to their standard to include a link to the
  1323. day's most popular book (in terms of visitor traffic). Here's
  1324. an Extension which adds a
  1325. <methodname>getDaysPopularBookLink()</methodname> method to the
  1326. feel level <acronym>API</acronym>.
  1327. </para>
  1328. <programlisting language="php"><![CDATA[
  1329. class My_FeedReader_Extension_JungleBooks_Feed
  1330. extends Zend_Feed_Reader_Extension_FeedAbstract
  1331. {
  1332. public function getDaysPopularBookLink()
  1333. {
  1334. if (isset($this->_data['dayPopular'])) {
  1335. return $this->_data['dayPopular'];
  1336. }
  1337. $dayPopular = $this->_xpath->evaluate(
  1338. 'string(' . $this->getXpathPrefix() . '/jungle:dayPopular)'
  1339. );
  1340. if (!$dayPopular) {
  1341. $dayPopular = null;
  1342. }
  1343. $this->_data['dayPopular'] = $dayPopular;
  1344. return $this->_data['dayPopular'];
  1345. }
  1346. protected function _registerNamespaces()
  1347. {
  1348. $this->_xpath->registerNamespace(
  1349. 'jungle', 'http://example.com/junglebooks/rss/module/1.0/'
  1350. );
  1351. }
  1352. }
  1353. ]]></programlisting>
  1354. <para>
  1355. Let's repeat the last example using a custom Extension to show the
  1356. method being used.
  1357. </para>
  1358. <programlisting language="php"><![CDATA[
  1359. if(!Zend_Feed_Reader::isRegistered('JungleBooks')) {
  1360. Zend_Feed_Reader::addPrefixPath(
  1361. '/path/to/My/FeedReader/Extension', 'My_FeedReader_Extension'
  1362. );
  1363. Zend_Feed_Reader::registerExtension('JungleBooks');
  1364. }
  1365. $feed = Zend_Feed_Reader::import('http://example.com/junglebooks/rss');
  1366. // URI to the information page of the day's most popular book with visitors
  1367. $daysPopularBookLink = $feed->getDaysPopularBookLink();
  1368. // ISBN for whatever book the first entry in the feed was concerned with
  1369. $firstIsbn = $feed->current()->getIsbn();
  1370. ]]></programlisting>
  1371. <para>
  1372. Going through these examples, you'll note that we don't register
  1373. feed and entry Extensions separately. Extensions within the same
  1374. standard may or may not include both a feed and entry class, so
  1375. <classname>Zend_Feed_Reader</classname> only requires you to
  1376. register the overall parent name, e.g. JungleBooks, DublinCore,
  1377. Slash. Internally, it can check at what level Extensions exist
  1378. and load them up if found. In our case, we have a full set of
  1379. Extensions now: <classname>JungleBooks_Feed</classname> and
  1380. <classname>JungleBooks_Entry</classname>.
  1381. </para>
  1382. </sect3>
  1383. </sect2>
  1384. </sect1>