Really Simple RSS

The newsboxes on the site are live, all in about a dozen lines of PHP. The backend to this is the extremely useful Magpie RSS parser tool ( http://magpierss.sourceforge.net/ ) which, at its simplest level, takes an RSS URL and spits out a code object representing the feed.

Since the whole point of this site is to be self-documentary, here's how I've done it:

1) Download magpieRSS (in this case, v0.52) from sourceforge.
2) Unzip it (tar -xvzf) into the web directory
3) Symlink around the version number (ln -s magpierss-0.5.2 magpie)
4) Edit the fetch.inc file to set up caching - this takes a few DEFINE statements that you can add anywhere in the code path, but that I tend to add in the "constants" section of the rss_fetch.inc file in Magpie:
define('MAGPIE_CACHE_ON',TRUE);
define('MAGPIE_DEBUG',TRUE); #this one's optional
define('MAGPIE_CACHE_DIR',MAGPIE_DIR . '/cache');
define('MAGPIE_CACHE_AGE',900);


5) Create the specified cache dir and make it writeable by the webserver.
6) Use the following function to generate really simple newsboxes:

require('magpie/rss_fetch.inc');

function rssbox($feedurl,$items=5) {
static $boxnum=0;
$boxnum++;
$rss=fetch_rss($feedurl);
if($rss->ERROR) { return("<p>RSS error: $rss->ERROR</p>"); }
$ret=("<div class=\"rssbox\" id=\"rssbox$boxnum\">
<div class=\"rsstitle\"><a href=\"".
($rss->channel['link'])."\">".
($rss->channel['title'])."</a></div>\n");
for($i=0;(($i+1)<count($rss->items) && ($i<$items));$i++) {
$ret.=("<div class=\"rssstory\"><a href=\"".
($rss->items[$i]['link'])."\">".
($rss->items[$i]['title'])."</a></div>\n");
}

$ret.=("</div>");
return($ret);
}

7) and call it with print(rss($rssurl));

8) Style as desired in CSS :)

Of course it's not nearly so powerful as my rss viewer app ( http://rssdisplay.infinitepenguins.net/ ) , but then that produces some semantically horrible code at the moment, so rewriting it in full will be one of the objectives for this site.
Posted by parsingphase, 2004-04-08 11:56

Anonymous user

Login

Blog

Contact

I'm currently available for contract work in London as a Senior PHP Developer. Contact me for a CV, rates, or a chat.

Twitter @parsingphase
Email richard@phase.org
Github parsingphase
LinkedIn Richard George
Flickr parsingphase