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