+
+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.
+
+
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:
+
+
+# you can also do this in your php.ini file
+ini_set('display_errors', 0);
+
+
+This recipe only works for RSS 1.0 feeds that include the field.
+(which is very good RSS style)
+parse_w3cdtf() is defined in
+rss_utils.inc, and parses RSS style dates into Unix epoch
+seconds.
+
+
+MagpieRSS provides fetch_rss() 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?
+
+
Solution
+
+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;
+}
+
+
+
Discussion
+Here we are using MagpieRSS's RSS parser directly without the convience wrapper
+of fetch_rss(). 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.
+
+
improved error handling, more flexibility for script authors,
+backwards compatible
+
new and better examples! including using MagpieRSS and Smarty
+
new Smarty plugin for RSS date parsing
+
+
+
+
+
+
+
+
+
Why?
+ I wrote MagpieRSS out of a frustration with the limitations of existing
+ solutions. In particular many of the existing PHP solutions seemed to:
+
+
use a parser based on regular expressions, making for an inherently
+ fragile solution
+
only support early versions of RSS
+
discard all the interesting information besides item title, description,
+ and link.
+
not build proper separation between parsing the RSS and displaying it.
+
+ In particular I failed to find any PHP RSS parsers that could sufficiently
+ parse RSS 1.0 feeds, to be useful on the RSS based event feeds we generate
+ at Protest.net.
+
+
+
+ Parses most RSS formats, including support for
+ 1.0 modules and limited
+ namespace support. RSS is packed into convenient data structures; easy to
+ use in PHP, and appropriate for passing to a templating system, like
+ Smarty.
+
+
+
Integrated Object Cache
+
+ Caching the parsed RSS means that the 2nd request is fast, and that
+including the rss_fetch call in your PHP page won't destroy your performance,
+and force you to reply on an external cron job. And it happens transparently.
+
+
+
+ Makes extensive use of constants to allow overriding default behaviour, and
+ installation on shared hosts.
+
+
Modular
+
+
rss_fetch.inc - wraps a simple interface (fetch_rss())
+ around the library.
+
rss_parse.inc - provides the RSS parser, and the RSS object
+
rss_cache.inc - a simple (no GC) object cache, optimized for RSS objects
+
rss_utils.inc - utility functions for working with RSS. currently
+ provides parse_w3cdtf(), for parsing W3CDTF into epoch seconds.
+
+
+
+
+
+
+
+
Magpie's approach to parsing RSS
+
+ Magpie takes a naive, and inclusive approach. Absolutely
+ non-validating, as long as the RSS feed is well formed, Magpie will
+ cheerfully parse new, and never before seen tags in your RSS feeds.
+
+
+ This makes it very simple support the varied versions of RSS simply, but
+ forces the consumer of a RSS feed to be cognizant of how it is
+ structured.(at least if you want to do something fancy)
+
+
+ Magpie parses a RSS feed into a simple object, with 4 fields:
+ channel, items, image, and
+ textinput.
+
+
+
channel
+ $rss->channel contains key-value pairs of all tags, without
+ nested tags, found between the root tag (<rdf:RDF>, or <rss>)
+ and the end of the document.
+
+
+
items
+ $rss->items is an array of associative arrays, each one
+ describing a single item. An example that looks like:
+
+<item rdf:about="http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257">
+<title>Weekly Peace Vigil</title>
+<link>http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257</link>
+<description>Wear a white ribbon</description>
+<dc:subject>Peace</dc:subject>
+<ev:startdate>2002-06-01T11:00:00</ev:startdate>
+<ev:location>Northampton, MA</ev:location>
+<ev:enddate>2002-06-01T12:00:00</ev:enddate>
+<ev:type>Protest</ev:type>
+</item>
+
+ Is parsed, and pushed on the $rss->items array as:
+
+array(
+ title => 'Weekly Peace Vigil',
+ link => 'http://protest.net/NorthEast/calendrome.cgi?span=event&ID=210257',
+ description => 'Wear a white ribbon',
+ dc => array (
+ subject => 'Peace'
+ ),
+ ev => array (
+ startdate => '2002-06-01T11:00:00',
+ enddate => '2002-06-01T12:00:00',
+ type => 'Protest',
+ location => 'Northampton, MA'
+ )
+);
+
+
+
+
image and textinput
+$rss->image and $rss-textinput are associative arrays
+including name-value pairs for anything found between the respective parent
+tags.
+
+