cookbook 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. MAGPIERSS RECIPES: Cooking with Corbies
  2. "Four and twenty blackbirds baked in a pie."
  3. 1. LIMIT THE NUMBER OF HEADLINES(AKA ITEMS) RETURNED.
  4. PROBLEM:
  5. You want to display the 10 (or 3) most recent headlines, but the RSS feed
  6. contains 15.
  7. SOLUTION:
  8. $num_items = 10;
  9. $rss = fetch_rss($url);
  10. $items = array_slice($rss->items, 0, $num_items);
  11. DISCUSSION:
  12. Rather then trying to limit the number of items Magpie parses, a much simpler,
  13. and more flexible approach is to take a "slice" of the array of items. And
  14. array_slice() is smart enough to do the right thing if the feed has less items
  15. then $num_items.
  16. See: http://www.php.net/array_slice
  17. 2. DISPLAY A CUSTOM ERROR MESSAGE IF SOMETHING GOES WRONG
  18. PROBLEM:
  19. You don't want Magpie's error messages showing up if something goes wrong.
  20. SOLUTION:
  21. # Magpie throws USER_WARNINGS only
  22. # so you can cloak these, by only showing ERRORs
  23. error_reporting(E_ERROR);
  24. # check the return value of fetch_rss()
  25. $rss = fetch_rss($url);
  26. if ( $rss ) {
  27. ...display rss feed...
  28. }
  29. else {
  30. echo "An error occured! " .
  31. "Consider donating more $$$ for restoration of services." .
  32. "<br>Error Message: " . magpie_error();
  33. }
  34. DISCUSSION:
  35. MagpieRSS triggers a warning in a number of circumstances. The 2 most common
  36. circumstances are: if the specified RSS file isn't properly formed (usually
  37. because it includes illegal HTML), or if Magpie can't download the remote RSS
  38. file, and there is no cached version.
  39. If you don't want your users to see these warnings change your error_reporting
  40. settings to only display ERRORs. Another option is to turn off display_error,
  41. so that WARNINGs, and NOTICEs still go to the error_log but not to the webpages.
  42. You can do this with:
  43. ini_set('display_errors', 0);
  44. See: http://www.php.net/error_reporting,
  45. http://www.php.net/ini_set,
  46. http://www.php.net/manual/en/ref.errorfunc.php
  47. 3. GENERATE A NEW RSS FEED
  48. PROBLEM:
  49. Create an RSS feed for other people to use.
  50. SOLUTION:
  51. Use Useful Inc's RSSWriter (http://usefulinc.com/rss/rsswriter/)
  52. DISCUSSION:
  53. An example of turning a Magpie parsed RSS object back into an RSS file is forth
  54. coming. In the meantime RSSWriter has great documentation.
  55. 4. DISPLAY HEADLINES MORE RECENT THEN X DATE
  56. PROBLEM:
  57. You only want to display headlines that were published on, or after a certain
  58. date.
  59. SOLUTION:
  60. require 'rss_utils.inc';
  61. # get all headlines published today
  62. $today = getdate();
  63. # today, 12AM
  64. $date = mktime(0,0,0,$today['mon'], $today['mday'], $today['year']);
  65. $rss = fetch_rss($url);
  66. foreach ( $rss->items as $item ) {
  67. $published = parse_w3cdtf($item['dc']['date']);
  68. if ( $published >= $date ) {
  69. echo "Title: " . $item['title'];
  70. echo "Published: " . date("h:i:s A", $published);
  71. echo "<p>";
  72. }
  73. }
  74. DISCUSSION:
  75. This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
  76. (which is very good RSS style)
  77. parse_w3cdtf is defined in rss_utils.inc, and parses RSS style dates into Unix
  78. epoch seconds.
  79. See: http://www.php.net/manual/en/ref.datetime.php