topical media & game development
mashup-delicious-06-example6-7-magpierss-htdocs-cookbook.htm / htm
<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>
rss = fetch_rss(items = array_slice(num_items);
foreach ( 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 rss = fetch_rss(rss ) {
...display rss feed...
}
else {
echo "An error occured! " .
"Consider donating more $$today = getdate();
# today, 12AM
today['mon'], today['year']);
url);
foreach ( item ) {
item['dc']['date']);
if ( date ) {
echo "Title: " . published);
echo "<p>";
}
}
</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 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_string = read_file(rss = new MagpieRSS( rss and !rss->ERROR;
}
# efficiently read a file into a string
# in php >= 4.3.0 you can simply use file_get_contents()
#
function read_file(fh = fopen(php_errormsg);
fh, filesize(fh);
return
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.