By Martin Stut, 2009-01-24

Rationale for a PHP Feedreader

Quite often, organizational websites are written in static HTML, which is fine for the purpose, but want to run some kind of news system to publish current events without having to edit the core pages. If a CMS is available, one can use that for the purpose. But if they do not want to run a CMS - there may be good reasons, e.g. security - there is the option of using a blog service like blogger.com as a backend. But what to use as a frontend, visible to website visitors?
Telling people "please get a feed reader and subscribe to our blog" is creating an inadequate obstacle - many visitors of the target audience are barely capable of typing a URL into their browser, let alone manage bookmarks. So the feed needs to display automatically with the webpage. Including a JavaScript feed reader would mean to require a client side script to pull data from a third-party server (blogger.com), which is for good reasons blocked by default due to the "same origin policy".
So the feed needs to be included by a server-side script. Nowadays PHP is available even with cheap shared hosting packages, so I could set out to create a PHP page blogfeed.php that pulls and displays the relevant items of the feed.

Magpie RSS

This task can't be unique, so I looked for a PHP script doing that. I found most references for Magpie rss (http://magpierss.sourceforge.net/). This homepage also gives links to hints useful for RSS users and webmasters in general.
The magpie project does not offer a real manual, but it does provide links to examples.
The provided example only shows how to display a list of titles. My plan is to show the first three or so news items in full text and only headlines for the rest.
The author of http://www.hiash.com/blogger.php (referred to on the Magpie links page) has exactly the same plans a I did: using blogger.com as a backend for another webpage.
That made me confident enough to try it. At first I did it on my home (intranet) web server:

My First Try with Magpie

I downloaded magpie in its most recent version 0.72 from the Sourceforge download page. There I discovered that the current version dates from 2005. I researched a bit for alternatives, but found only very few, very simple (probably too simple) scripts, e.g. LastRSS. So I concluded, that Magpie's age may be a sign of matureness and robustness, rather than obsoleteness.
All magpie users recommend this directory structure, so I set it up too:

magpie/rss_cache.inc
magpie/rss_fetch.inc
magpie/rss_parse.inc
magpie/rss_utils.inc
magpie/extlib/Snoopy.class.inc

The untarred download contains a nice file htdocs/cookbook.html.

For a first try, I created a little file http://www.home.stut.de/newsfeed.php (home intranet only) on the root directory of my home intranet web server (Apache with PHP5 on Debian Etch), copied almost literally from hiash.com:
define('MAGPIE_DIR', 'magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');
$rss = fetch_rss( 'http://stut-tech.blogspot.com/feeds/posts/default' );
//display latest blog content:
$item = $rss->items[0];
$content = $item['atom_content'];
echo "
Latest Blog Entry:
$content
\n";

It really does show my last blog post. But I want to know more about the inner workings, so I try to look at the complete array contents using the print_r function:

MagpieRSS Object([parser] => Resource id #9
[current_item] => Array()
[items] => Array([0] => Array(
  [id] => tag:blogger.com,1999:blog-3769163305311983690.post-6898517931677696103
  [published] => 2009-01-17T15:25:00.003+01:00
  [updated] => 2009-01-17T16:07:54.525+01:00
  [app] => Array([edited] => 2009-01-17T16:07:54.525+01:00)
  [title] => jbrout Photo Manager[atom_content] => I'm no longer ...  (full length article cut for brevity)... you can really find the buried gems in your picture collection.
  [link_related] => http://jbrout.free.fr/
  [link_replies] => http://stut-tech.blogspot.com/feeds/6898517931677696103/comments/defaulthttp://stut-tech.blogspot.com/2009/01/jbrout-photo-manager.html#comment-form
  [link_edit] => http://www.blogger.com/feeds/3769163305311983690/posts/default/6898517931677696103?v=2
  [link_self] => http://stut-tech.blogspot.com/feeds/posts/default/6898517931677696103
  [link] => http://stut-tech.blogspot.com/2009/01/jbrout-photo-manager.html
  [author_name] => Martin Stut
  [author_uri] => http://www.blogger.com/profile/09103658031822073197
  [author_email] => noreply@blogger.com
  [thr] => Array([total] => 0)
)
[1] => Array(
  [id] => tag:blogger.com,1999:blog-3769163305311983690.post-1064215087336461038
  [published] => 2009-01-14T19:51:00.004+01:00
  [updated] => 2009-01-17T15:24:55.524+01:00
  [app] => Array([edited] => 2009-01-17T15:24:55.524+01:00)
  [title] => Copy Audio CDs
  [atom_content] => (full length article omitted for brevity)
  [link_replies] => http://stut-tech.blogspot.com/feeds/1064215087336461038/comments/default http://stut-tech.blogspot.com/2009/01/copy-audio-cds.html#comment-form
  [link_edit] => http://www.blogger.com/feeds/3769163305311983690/posts/default/1064215087336461038?v=2
  [link_self] => http://stut-tech.blogspot.com/feeds/posts/default/1064215087336461038
  [link] => http://stut-tech.blogspot.com/2009/01/copy-audio-cds.html
  [author_name] => Martin Stut
  [author_uri] => http://www.blogger.com/profile/09103658031822073197
  [author_email] => noreply@blogger.com
  [thr] => Array([total] => 0)
)
)
[channel] => Array(
  [id] => tag:blogger.com,1999:blog-3769163305311983690
  [updated] => 2009-01-18T07:13:25.599+01:00
  [title] => Martin Stut's IT Experiments
  [subtitle] => Almost every Saturday, I'm experimenting with some IT stuff, usually open source software on Debian Linux. I'm planning to document and share my findings here. Tests of software, tweaks needed to get something going, bringing a buried gem to the attention it might deserve.
  [link_http://schemas.google.com/g/2005#feed] => http://stut-tech.blogspot.com/feeds/posts/default
  [link_self] => http://stut-tech.blogspot.com/feeds/posts/default
  [link] => http://stut-tech.blogspot.com/
  [author_name] => Martin Stut
  [author_uri] => http://www.blogger.com/profile/09103658031822073197
  [author_email] => noreply@blogger.com
  [generator] => Blogger
  [opensearch] => Array([totalresults] => 2
    [startindex] => 1
    [itemsperpage] => 25)
)
[textinput] => Array()
[image] => Array()
[feed_type] => Atom
[feed_version] =>
[encoding] => ISO-8859-1
[_source_encoding] =>
[ERROR] =>
[WARNING] =>
[_CONTENT_CONSTRUCTS] => Array(
  [0] => content
  [1] => summary
  [2] => info
  [3] => title
  [4] => tagline
  [5] => copyright)
[_KNOWN_ENCODINGS] => Array([0] => UTF-8; [1] => US-ASCII; [2] => ISO-8859-1)
[stack] => Array()
[inchannel] =>
[initem] =>
[incontent] =>
[intextinput] =>
[inimage] =>
[current_namespace] =>
[last_modified] => Sun, 18 Jan 2009 06:13:25 GMT
[etag] => W/"A0UERHsycSp7ImA9WxVREkg."
)

Adding the newsfeed to an existing HTML-only website

I added a magpie folder (chmod 755) to the root directory of my website, and a blogfeed.php file to the root level of it.
I was able to call blogfeed.php in Firefox, but non-ASCII characters were broken. I solved this by adding a meta tag to the head section, so the charset became defined.

Blogfeed on stut.de


Current Posts in my Tech-Blog

'.$item['title']."

\n"; echo " ".$item['atom_content']." \n"; } // only for the first item after the completely displayed ones: if ($itemCounter == $numberOfFullyDisplayedItems+1) { echo "

More Items

\n"; echo "

\n"; } } // if there were more items: close the UL if ($itemCounter > $numberOfFullyDisplayedItems) { echo " \n"; } ?>

Now this code needs to get on the front page. The first obvious way would be to include it on index.php - but what if the start page is called index.html and changing the name to index.php is not an easy option?
Answer: Include blogfeed.php as an IFRAME. This is straightforward (if you are fluent in HTML and CSS):

Portrait of Martin Stut (JPG 5.5kB)
Current
posts of my tech-blog

You can view the result, together with a 1996 photo of me, at http://www.stut.de/martin.html