[12] | 1 | MAGPIERSS RECIPES: Cooking with Corbies
|
---|
| 2 |
|
---|
| 3 | "Four and twenty blackbirds baked in a pie."
|
---|
| 4 |
|
---|
| 5 | 1. LIMIT THE NUMBER OF HEADLINES(AKA ITEMS) RETURNED.
|
---|
| 6 |
|
---|
| 7 | PROBLEM:
|
---|
| 8 |
|
---|
| 9 | You want to display the 10 (or 3) most recent headlines, but the RSS feed
|
---|
| 10 | contains 15.
|
---|
| 11 |
|
---|
| 12 | SOLUTION:
|
---|
| 13 |
|
---|
| 14 | $num_items = 10;
|
---|
| 15 | $rss = fetch_rss($url);
|
---|
| 16 |
|
---|
| 17 | $items = array_slice($rss->items, 0, $num_items);
|
---|
| 18 |
|
---|
| 19 | DISCUSSION:
|
---|
| 20 |
|
---|
| 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
|
---|
| 24 | then $num_items.
|
---|
| 25 |
|
---|
| 26 | See: http://www.php.net/array_slice
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | 2. DISPLAY A CUSTOM ERROR MESSAGE IF SOMETHING GOES WRONG
|
---|
| 30 |
|
---|
| 31 | PROBLEM:
|
---|
| 32 |
|
---|
| 33 | You don't want Magpie's error messages showing up if something goes wrong.
|
---|
| 34 |
|
---|
| 35 | SOLUTION:
|
---|
| 36 |
|
---|
| 37 | # Magpie throws USER_WARNINGS only
|
---|
| 38 | # so you can cloak these, by only showing ERRORs
|
---|
| 39 | error_reporting(E_ERROR);
|
---|
| 40 |
|
---|
| 41 | # check the return value of fetch_rss()
|
---|
| 42 |
|
---|
| 43 | $rss = fetch_rss($url);
|
---|
| 44 |
|
---|
| 45 | if ( $rss ) {
|
---|
| 46 | ...display rss feed...
|
---|
| 47 | }
|
---|
| 48 | else {
|
---|
| 49 | echo "An error occured! " .
|
---|
| 50 | "Consider donating more $$$ for restoration of services." .
|
---|
| 51 | "<br>Error Message: " . magpie_error();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | DISCUSSION:
|
---|
| 55 |
|
---|
| 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.
|
---|
| 60 |
|
---|
| 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.
|
---|
| 64 |
|
---|
| 65 | You can do this with:
|
---|
| 66 |
|
---|
| 67 | ini_set('display_errors', 0);
|
---|
| 68 |
|
---|
| 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
|
---|
| 72 |
|
---|
| 73 | 3. GENERATE A NEW RSS FEED
|
---|
| 74 |
|
---|
| 75 | PROBLEM:
|
---|
| 76 |
|
---|
| 77 | Create an RSS feed for other people to use.
|
---|
| 78 |
|
---|
| 79 | SOLUTION:
|
---|
| 80 |
|
---|
| 81 | Use Useful Inc's RSSWriter (http://usefulinc.com/rss/rsswriter/)
|
---|
| 82 |
|
---|
| 83 | DISCUSSION:
|
---|
| 84 |
|
---|
| 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.
|
---|
| 87 |
|
---|
| 88 | 4. DISPLAY HEADLINES MORE RECENT THEN X DATE
|
---|
| 89 |
|
---|
| 90 | PROBLEM:
|
---|
| 91 |
|
---|
| 92 | You only want to display headlines that were published on, or after a certain
|
---|
| 93 | date.
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | SOLUTION:
|
---|
| 97 |
|
---|
| 98 | require '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 |
|
---|
| 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);
|
---|
| 113 | echo "<p>";
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | DISCUSSION:
|
---|
| 118 |
|
---|
| 119 | This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
|
---|
| 120 | (which is very good RSS style)
|
---|
| 121 |
|
---|
| 122 | parse_w3cdtf is defined in rss_utils.inc, and parses RSS style dates into Unix
|
---|
| 123 | epoch seconds.
|
---|
| 124 |
|
---|
| 125 | See: http://www.php.net/manual/en/ref.datetime.php
|
---|