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