/www/magpierss-0.72/htdocs/cookbook.html

# · HTML · 237 lines · 171 code · 53 blank · 13 comment · 0 complexity · d444251435fedb4017b1d25a2433f127 MD5 · raw file

  1. <html>
  2. <head>
  3. <title>Magie RSS Recipes: Simple PHP RSS How To</title>
  4. <style>
  5. body {
  6. font-family:trebuchet MS, trebuchet, verdana, arial, sans-serif;
  7. font-size: 11px;
  8. }
  9. pre { font-family: "Courier New", monospace;
  10. padding: 1em;
  11. margin: 0.2em 2.5em 0.2em 3em;
  12. background-color: #efeff5;
  13. border: 1px solid #cfcfcf;
  14. white-space: pre;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <p>
  20. <h1>MagpieRSS Recipes: Cooking with Corbies</h1>
  21. <div align="center"><h3><em>"Four and twenty blackbirds baked in a
  22. pie."</em></h3></div>
  23. </p>
  24. <p>
  25. <ol>
  26. <li><a href="#limit">Limit the Number of Headlines(aka Items) Returned</a></li>
  27. <li><a href="#error_message">Display a Custom Error Message if Something Goes
  28. Wrong</a></li>
  29. <li><a href="#write_rss">Generate a New RSS Feed</a></li>
  30. <li><a href="#by_date">Display Headlines More Recent then X Date</a></li>
  31. <li><a href="#from_file">Parse a Local File Containing RSS</a></li>
  32. </ol>
  33. </p>
  34. <a name="limit"></a><h2>1. Limit the Number of Headlines(aka Items) Returned.</h2>
  35. <h3>Problem:</h3>
  36. You want to display the 10 (or 3 or whatever) most recent headlines, but the RSS feed
  37. contains 15.
  38. <h3>Solution:</h3>
  39. <pre>
  40. $num_items = 10;
  41. $rss = fetch_rss($url);
  42. $items = array_slice($rss->items, 0, $num_items);
  43. foreach ( $items as $item ) {
  44. </pre>
  45. <h3>Discussion:</h3>
  46. Rather then trying to limit the number of items Magpie parses, a much simpler,
  47. and more flexible approach is to take a "slice" of the array of items. And
  48. array_slice() is smart enough to do the right thing if the feed has less items
  49. then $num_items.
  50. <h3>See:</h3> <a href="http://www.php.net/array_slice">http://www.php.net/array_slice</a>
  51. </p>
  52. <a name="error_message"></a><h2>2. Display a Custom Error Message if Something Goes Wrong</h2>
  53. <h3>Problem:</h3>
  54. You don't want Magpie's error messages showing up if something goes wrong.
  55. <h3>Solution:</h3>
  56. <pre>
  57. # Magpie throws USER_WARNINGS only
  58. # so you can cloak these, by only showing ERRORs
  59. error_reporting(E_ERROR);
  60. # check the return value of fetch_rss()
  61. $rss = fetch_rss($url);
  62. if ( $rss ) {
  63. ...display rss feed...
  64. }
  65. else {
  66. echo "An error occured! " .
  67. "Consider donating more $$$ for restoration of services." .
  68. "&lt;br&gt;Error Message: " . magpie_error();
  69. }
  70. </pre>
  71. <h3>Discussion:</h3>
  72. MagpieRSS triggers a warning in a number of circumstances. The 2 most common
  73. circumstances are: if the specified RSS file isn't properly formed (usually
  74. because it includes illegal HTML), or if Magpie can't download the remote RSS
  75. file, and there is no cached version.
  76. If you don't want your users to see these warnings change your error_reporting
  77. settings to only display ERRORs.<br />
  78. Another option is to turn off display_error,
  79. so that WARNINGs, and NOTICEs still go to the error_log but not to the webpages.
  80. You can do this with:
  81. <pre>
  82. # you can also do this in your php.ini file
  83. ini_set('display_errors', 0);
  84. </pre>
  85. <h3>See:</h3>
  86. <a
  87. href="http://www.php.net/error_reporting">http://www.php.net/error_reporting</a>,<br
  88. />
  89. <a href="http://www.php.net/ini_set">http://www.php.net/ini_set</a>, <br />
  90. <a
  91. href="http://www.php.net/manual/en/ref.errorfunc.php">http://www.php.net/manual/en/ref.errorfunc.php</a><br
  92. />
  93. <a name="write_rss"></a><h2>3. Generate a New RSS Feed</h2>
  94. <h3>Problem:</h3>
  95. Create an RSS feed for other people to use.
  96. <h3>Solution:</h3>
  97. Use Useful Inc's <a href="http://usefulinc.com/rss/rsswriter/">RSSWriter</a>.
  98. <h3>Discussion:</h3>
  99. An example of turning a Magpie parsed RSS object back into an RSS file is
  100. forthcoming. In the meantime RSSWriter is well documented.
  101. <a name="by_date"></a><h2>4. Display Headlines More Recent then X Date</h2>
  102. <h3>Problem:</h3>
  103. You only want to display headlines that were published on, or after a certain
  104. date.
  105. <h3>Solution:</h3>
  106. <pre>
  107. require_once('rss_utils.inc');
  108. # get all headlines published today
  109. $today = getdate();
  110. # today, 12AM
  111. $date = mktime(0,0,0,$today['mon'], $today['mday'], $today['year']);
  112. $rss = fetch_rss($url);
  113. foreach ( $rss->items as $item ) {
  114. $published = parse_w3cdtf($item['dc']['date']);
  115. if ( $published &gt;= $date ) {
  116. echo "Title: " . $item['title'];
  117. echo "Published: " . date("h:i:s A", $published);
  118. echo "&lt;p&gt;";
  119. }
  120. }
  121. </pre>
  122. <h3>Discussion:</h3>
  123. This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
  124. (which is very good RSS style) <br />
  125. <code>parse_w3cdtf()</code> is defined in
  126. <code>rss_utils.inc</code>, and parses RSS style dates into Unix epoch
  127. seconds.
  128. <h3>See: </h3>
  129. <a
  130. href="http://www.php.net/manual/en/ref.datetime.php">http://www.php.net/manual/en/ref.datetime.php</a>
  131. <a name="from_file"></a>
  132. <h2>5. Parse a Local File Containing RSS</h2>
  133. <h3>Problem:</h3>
  134. MagpieRSS provides <code>fetch_rss()</code> which takes a URL and returns a
  135. parsed RSS object, but what if you want to parse a file stored locally that
  136. doesn't have a URL?
  137. <h3>Solution</h3>
  138. <pre>
  139. require_once('rss_parse.inc');
  140. $rss_file = 'some_rss_file.rdf';
  141. $rss_string = read_file($rss_file);
  142. $rss = new MagpieRSS( $rss_string );
  143. if ( $rss and !$rss->ERROR) {
  144. ...display rss...
  145. }
  146. else {
  147. echo "Error: " . $rss->ERROR;
  148. }
  149. # efficiently read a file into a string
  150. # in php >= 4.3.0 you can simply use file_get_contents()
  151. #
  152. function read_file($filename) {
  153. $fh = fopen($filename, 'r') or die($php_errormsg);
  154. $rss_string = fread($fh, filesize($filename) );
  155. fclose($fh);
  156. return $rss_string;
  157. }
  158. </pre>
  159. <h3>Discussion</h3>
  160. Here we are using MagpieRSS's RSS parser directly without the convience wrapper
  161. of <code>fetch_rss()</code>. We read the contents of the RSS file into a
  162. string, and pass it to the parser constructor. Notice also that error handling
  163. is subtly different.
  164. <h3>See: </h3>
  165. <a
  166. href="http://www.php.net/manual/en/ref.filesystem.php">http://www.php.net/manual/en/ref.filesystem.php</a>,<br
  167. />
  168. <a
  169. href="http://www.php.net/manual/en/language.oop.php">http://www.php.net/manual/en/language.oop.php</a>
  170. <!--
  171. <a name="link"></a><h2>#. Recipe</h2>
  172. <h3>Problem:</h3>
  173. Problem description
  174. <h3>Solution</h3>
  175. <pre>
  176. code
  177. </pre>
  178. <h3>Discussion/h3>
  179. Discuss code
  180. <h3>See: </h3>
  181. Documentation links:
  182. -->
  183. </body>
  184. </html>