3 <title>Magie RSS Recipes: Simple PHP RSS How To
</title>
6 font-family:trebuchet MS, trebuchet, verdana, arial, sans-serif;
11 pre { font-family:
"Courier New", monospace;
13 margin:
0.2em
2.5em
0.2em
3em;
14 background-color: #efeff5;
15 border:
1px solid #cfcfcf;
23 <h1>MagpieRSS Recipes: Cooking with Corbies
</h1>
25 <div align=
"center"><h3><em>"Four and twenty blackbirds baked in a
30 <li><a href=
"#limit">Limit the Number of Headlines(aka Items) Returned
</a></li>
31 <li><a href=
"#error_message">Display a Custom Error Message if Something Goes
33 <li><a href=
"#write_rss">Generate a New RSS Feed
</a></li>
34 <li><a href=
"#by_date">Display Headlines More Recent then X Date
</a></li>
35 <li><a href=
"#from_file">Parse a Local File Containing RSS
</a></li>
40 <a name=
"limit"></a><h2>1. Limit the Number of Headlines(aka Items) Returned.
</h2>
44 You want to display the
10 (or
3 or whatever) most recent headlines, but the RSS feed
51 $rss = fetch_rss($url);
53 $items = array_slice($rss-
>items,
0, $num_items);
55 foreach ( $items as $item ) {
59 Rather then trying to limit the number of items Magpie parses, a much simpler,
60 and more flexible approach is to take a
"slice" of the array of items. And
61 array_slice() is smart enough to do the right thing if the feed has less items
64 <h3>See:
</h3> <a href=
"http://www.php.net/array_slice">http://www.php.net/array_slice
</a>
67 <a name=
"error_message"></a><h2>2. Display a Custom Error Message if Something Goes Wrong
</h2>
71 You don't want Magpie's error messages showing up if something goes wrong.
75 # Magpie throws USER_WARNINGS only
76 # so you can cloak these, by only showing ERRORs
77 error_reporting(E_ERROR);
79 # check the return value of fetch_rss()
81 $rss = fetch_rss($url);
84 ...display rss feed...
87 echo
"An error occured! " .
88 "Consider donating more $$$ for restoration of services." .
89 "<br>Error Message: " . magpie_error();
94 MagpieRSS triggers a warning in a number of circumstances. The
2 most common
95 circumstances are: if the specified RSS file isn't properly formed (usually
96 because it includes illegal HTML), or if Magpie can't download the remote RSS
97 file, and there is no cached version.
99 If you don't want your users to see these warnings change your error_reporting
100 settings to only display ERRORs.
<br />
101 Another option is to turn off display_error,
102 so that WARNINGs, and NOTICEs still go to the error_log but not to the webpages.
104 You can do this with:
107 # you can also do this in your php.ini file
108 ini_set('display_errors',
0);
113 href=
"http://www.php.net/error_reporting">http://www.php.net/error_reporting
</a>,
<br
115 <a href=
"http://www.php.net/ini_set">http://www.php.net/ini_set
</a>,
<br />
117 href=
"http://www.php.net/manual/en/ref.errorfunc.php">http://www.php.net/manual/en/ref.errorfunc.php
</a><br
120 <a name=
"write_rss"></a><h2>3. Generate a New RSS Feed
</h2>
124 Create an RSS feed for other people to use.
128 Use Useful Inc's
<a href=
"http://usefulinc.com/rss/rsswriter/">RSSWriter
</a>.
132 An example of turning a Magpie parsed RSS object back into an RSS file is
133 forthcoming. In the meantime RSSWriter is well documented.
135 <a name=
"by_date"></a><h2>4. Display Headlines More Recent then X Date
</h2>
139 You only want to display headlines that were published on, or after a certain
145 require_once('rss_utils.inc');
147 # get all headlines published today
151 $date = mktime(
0,
0,
0,$today['mon'], $today['mday'], $today['year']);
153 $rss = fetch_rss($url);
155 foreach ( $rss-
>items as $item ) {
156 $published = parse_w3cdtf($item['dc']['date']);
157 if ( $published
>= $date ) {
158 echo
"Title: " . $item['title'];
159 echo
"Published: " . date(
"h:i:s A", $published);
166 This recipe only works for RSS
1.0 feeds that include the
<dc:date> field.
167 (which is very good RSS style)
<br />
168 <code>parse_w3cdtf()
</code> is defined in
169 <code>rss_utils.inc
</code>, and parses RSS style dates into Unix epoch
174 href=
"http://www.php.net/manual/en/ref.datetime.php">http://www.php.net/manual/en/ref.datetime.php
</a>
176 <a name=
"from_file"></a>
177 <h2>5. Parse a Local File Containing RSS
</h2>
179 MagpieRSS provides
<code>fetch_rss()
</code> which takes a URL and returns a
180 parsed RSS object, but what if you want to parse a file stored locally that
185 require_once('rss_parse.inc');
187 $rss_file = 'some_rss_file.rdf';
188 $rss_string = read_file($rss_file);
189 $rss = new MagpieRSS( $rss_string );
191 if ( $rss and !$rss-
>ERROR) {
195 echo
"Error: " . $rss-
>ERROR;
198 # efficiently read a file into a string
199 # in php
>=
4.3.0 you can simply use file_get_contents()
201 function read_file($filename) {
202 $fh = fopen($filename, 'r') or die($php_errormsg);
203 $rss_string = fread($fh, filesize($filename) );
210 Here we are using MagpieRSS's RSS parser directly without the convience wrapper
211 of
<code>fetch_rss()
</code>. We read the contents of the RSS file into a
212 string, and pass it to the parser constructor. Notice also that error handling
217 href=
"http://www.php.net/manual/en/ref.filesystem.php">http://www.php.net/manual/en/ref.filesystem.php
</a>,
<br
220 href=
"http://www.php.net/manual/en/language.oop.php">http://www.php.net/manual/en/language.oop.php
</a>
223 <a name="link"></a><h2>#. Recipe</h2>