File: cookbook.html

package info (click to toggle)
magpierss 0.72-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 228 kB
  • ctags: 43
  • sloc: php: 1,483; makefile: 8
file content (237 lines) | stat: -rw-r--r-- 6,195 bytes parent folder | download | duplicates (12)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<html>
	<head>
	<title>Magie RSS Recipes:  Simple PHP RSS How To</title>
	<style>
	body {
		font-family:trebuchet MS, trebuchet, verdana, arial, sans-serif; 
		font-size: 11px; 
	
	}
	
	pre { font-family: "Courier New", monospace; 
      padding: 1em; 
      margin: 0.2em 2.5em 0.2em 3em;
      background-color: #efeff5;
      border: 1px solid #cfcfcf;
      white-space: pre;
 	}

	</style>
	</head>
	<body>
<p>
<h1>MagpieRSS Recipes:  Cooking with Corbies</h1>

<div align="center"><h3><em>"Four and twenty blackbirds baked in a
pie."</em></h3></div>
</p>
<p>
<ol>
<li><a href="#limit">Limit the Number of Headlines(aka Items) Returned</a></li>
<li><a href="#error_message">Display a Custom Error Message if Something Goes
Wrong</a></li>
<li><a href="#write_rss">Generate a New RSS Feed</a></li>
<li><a href="#by_date">Display Headlines More Recent then X Date</a></li>
<li><a href="#from_file">Parse a Local File Containing RSS</a></li>

</ol>
</p>

<a name="limit"></a><h2>1. Limit the Number of Headlines(aka Items) Returned.</h2>

<h3>Problem:</h3>

You want to display the 10 (or 3 or whatever) most recent headlines, but the RSS feed
contains 15.

<h3>Solution:</h3>

<pre>
$num_items = 10;
$rss = fetch_rss($url);

$items = array_slice($rss->items, 0, $num_items);

foreach ( $items as $item ) {
</pre>
<h3>Discussion:</h3>

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.

<h3>See:</h3> <a href="http://www.php.net/array_slice">http://www.php.net/array_slice</a>
</p>

<a name="error_message"></a><h2>2. Display a Custom Error Message if Something Goes Wrong</h2>

<h3>Problem:</h3>

You don't want Magpie's error messages showing up if something goes wrong.

<h3>Solution:</h3>
<pre>
# 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." .
        "&lt;br&gt;Error Message: " . magpie_error();
}
</pre>
<h3>Discussion:</h3>

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.<br />
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:

<pre>
# you can also do this in your php.ini file
ini_set('display_errors', 0);
</pre>

<h3>See:</h3>
<a
href="http://www.php.net/error_reporting">http://www.php.net/error_reporting</a>,<br
/>
<a href="http://www.php.net/ini_set">http://www.php.net/ini_set</a>, <br />
<a
href="http://www.php.net/manual/en/ref.errorfunc.php">http://www.php.net/manual/en/ref.errorfunc.php</a><br
/>

<a name="write_rss"></a><h2>3. Generate a New RSS Feed</h2>

<h3>Problem:</h3>

Create an RSS feed for other people to use.

<h3>Solution:</h3>

Use Useful Inc's <a href="http://usefulinc.com/rss/rsswriter/">RSSWriter</a>.

<h3>Discussion:</h3>

An example of turning a Magpie parsed RSS object back into an RSS file is
forthcoming.  In the meantime RSSWriter is well documented.

<a name="by_date"></a><h2>4. Display Headlines More Recent then X Date</h2>

<h3>Problem:</h3>

You only want to display headlines that were published on, or after a certain
date.


<h3>Solution:</h3>
<pre>
require_once('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 &gt;= $date ) {
        echo "Title: " . $item['title'];
        echo "Published: " . date("h:i:s A", $published);
        echo "&lt;p&gt;";
    }
}
</pre>
<h3>Discussion:</h3>

This recipe only works for RSS 1.0 feeds that include the <dc:date> field.
(which is very good RSS style) <br /> 
<code>parse_w3cdtf()</code> is defined in
<code>rss_utils.inc</code>, and parses RSS style dates into Unix epoch
seconds.  

<h3>See: </h3>
<a
href="http://www.php.net/manual/en/ref.datetime.php">http://www.php.net/manual/en/ref.datetime.php</a>

<a name="from_file"></a>
<h2>5. Parse a Local File Containing RSS</h2>
<h3>Problem:</h3>
MagpieRSS provides <code>fetch_rss()</code> which takes a URL and returns a
parsed RSS object, but what if you want to parse a file stored locally that
doesn't have a URL?

<h3>Solution</h3>
<pre>
require_once('rss_parse.inc');

$rss_file = 'some_rss_file.rdf';
$rss_string = read_file($rss_file);
$rss = new MagpieRSS( $rss_string );

if ( $rss and !$rss->ERROR) {
...display rss...
}
else {
    echo "Error: " . $rss->ERROR;
}

# efficiently read a file into a string
# in php >= 4.3.0 you can simply use file_get_contents()
#
function read_file($filename) {
    $fh = fopen($filename, 'r') or die($php_errormsg);
    $rss_string = fread($fh, filesize($filename) );
    fclose($fh);
    return $rss_string;
}
</pre>

<h3>Discussion</h3>
Here we are using MagpieRSS's RSS parser directly without the convience wrapper
of <code>fetch_rss()</code>.  We read the contents of the RSS file into a
string, and pass it to the parser constructor.  Notice also that error handling
is subtly different.

<h3>See: </h3>
<a
href="http://www.php.net/manual/en/ref.filesystem.php">http://www.php.net/manual/en/ref.filesystem.php</a>,<br
/>
<a
href="http://www.php.net/manual/en/language.oop.php">http://www.php.net/manual/en/language.oop.php</a>

<!-- 
<a name="link"></a><h2>#. Recipe</h2>
<h3>Problem:</h3>
Problem description
<h3>Solution</h3>
<pre>
code
</pre>
<h3>Discussion/h3>
Discuss code
<h3>See: </h3>
Documentation links:
-->

</body>
</html>