1 MAGPIERSS RECIPES: Cooking with Corbies
3 "Four and twenty blackbirds baked in a pie."
5 1. LIMIT THE NUMBER OF HEADLINES(AKA ITEMS) RETURNED.
9 You want to display the 10 (or 3) most recent headlines, but the RSS feed
15 $rss = fetch_rss($url);
17 $items = array_slice($rss->items, 0, $num_items);
21 Rather then trying to limit the number of items Magpie parses, a much simpler,
22 and more flexible approach is to take a "slice" of the array of items. And
23 array_slice() is smart enough to do the right thing if the feed has less items
26 See: http://www.php.net/array_slice
29 2. DISPLAY A CUSTOM ERROR MESSAGE IF SOMETHING GOES WRONG
33 You don't want Magpie's error messages showing up if something goes wrong.
37 # Magpie throws USER_WARNINGS only
38 # so you can cloak these, by only showing ERRORs
39 error_reporting(E_ERROR);
41 # check the return value of fetch_rss()
43 $rss = fetch_rss($url);
46 ...display rss feed...
49 echo "An error occured! " .
50 "Consider donating more $$$ for restoration of services." .
51 "<br>Error Message: " . magpie_error();
56 MagpieRSS triggers a warning in a number of circumstances. The 2 most common
57 circumstances are: if the specified RSS file isn't properly formed (usually
58 because it includes illegal HTML), or if Magpie can't download the remote RSS
59 file, and there is no cached version.
61 If you don't want your users to see these warnings change your error_reporting
62 settings to only display ERRORs. Another option is to turn off display_error,
63 so that WARNINGs, and NOTICEs still go to the error_log but not to the webpages.
67 ini_set('display_errors', 0);
69 See: http://www.php.net/error_reporting,
70 http://www.php.net/ini_set,
71 http://www.php.net/manual/en/ref.errorfunc.php
73 3. GENERATE A NEW RSS FEED
77 Create an RSS feed for other people to use.
81 Use Useful Inc's RSSWriter (http://usefulinc.com/rss/rsswriter/)
85 An example of turning a Magpie parsed RSS object back into an RSS file is forth
86 coming. In the meantime RSSWriter has great documentation.
88 4. DISPLAY HEADLINES MORE RECENT THEN X DATE
92 You only want to display headlines that were published on, or after a certain
98 require 'rss_utils.inc';
100 # get all headlines published today
104 $date = mktime(0,0,0,$today['mon'], $today['mday'], $today['year']);
106 $rss = fetch_rss($url);
108 foreach ( $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);
119 This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
120 (which is very good RSS style)
122 parse_w3cdtf is defined in rss_utils.inc, and parses RSS style dates into Unix
125 See: http://www.php.net/manual/en/ref.datetime.php