Using the Stumbleupon XML feed

Gavin, 22 May 04

These are some notes on how I got the Stumbleupon feed into my own blog. The finished effect (but now with BBC and del.icio.us RSS data) is being used for a page header on the main page of this site.

ALTERNATIVES

Before going into the details of setting this up with PHP it’s worth considering the alternatives. These may be useful if you are not able to install PHP scripts on your server. I have not used these myself.

There is a plugin for Movabletype that would appear to offer fairly simple way of getting the RSS feed into a blog – but only if the blog is MT powered.

Another way of incorporating the feed data is available at Feed Digest – this should be simpler to setup (especially for sites built with limited server access) – but your feed is then dependant on the feed digest servers as well as stumbleupon’s and your own.

It requires signing up with Feed Digest. Then you simply enter the feed url (in my case this would be http://stumbleupon.com/syndicate.php?stumbler=nog) in a form on their page to generate a line of javascript. Add this javascript to your blog page and it should retrieve information from the StumbleUpon feed. It should work with any blog where javascript can be added.

THE PHP WAY

The way I have incorporated the feed into the site uses some PHP code from Magpie RSS. Although some of the instructions relate to MoveableType, they are compatible with any CMS or blogging system allowing custom PHP – in fact nogwa.com now runs on Textpattern.

Anyway, I decided to try and jot down the steps needed for this before I forget. I think there are a few pointers on the Magpie site, or linked from it. I think a step by step guide will be useful should I (or someone else) wish to set something similar up in the future.

Download the magpie files from http://sourceforge.net/project/showfiles.php?group_id=55691

Version 0.72 is the current recommended release. It is only 36Kb compressed.

Uncompress the archive and upload as a complete directory to the server.
It would be reccommended if possible to put the magpie folder beneath the publicly accessible part of the site.

The files should not need changes to the permissions. All the inc files should be readable by all users, if they are not: chmod to 755.

Now it is time to edit the php index page. (With movable type, this should be done from within the mt configuration screens). It is probably the main index that will use php to get the data from the Magpie parser. (In MT this should be index.php).

MT can work with a combination of html / mt_tags and php.

The first line of code to add to the PHP page indicates that the magpie rss_fetch.inc file is to be included with the require_once function.
ie. add the require_once line after the php opening tag.

if magpie files are on the server in a folder below the html files, the path to the magpie directory must be given in full:

require_once '/user1/site1/magpie/rss_fetch.inc';

if in a folder that will be accessible on the web – the uri can be given eg.

require_once 'http://www.nogwa.com/magpie/rss_fetch.inc';

fetch_rss is code used to pass the url to the parser. The magpie parser will return several values including title,link,description.

eg. in my case url used would be http://www.stumbleupon.com/syndicate.php?stumbler=nog – this returns the most recent sites that you have thumbed as ‘I Like It’ with Stumbleupon
or for a fuller feed (echoing the contents of your blog):
http://www.stumbleupon.com/syndicate.php?stumbler=nog&comments=1 – this returns the stumbleupon blog in the data parsed by magpie. The code to incorporate the feed via Magpie should be something like this:

<?php
require_once '/user1/site1/magpie/rss_fetch.inc';
$url = 'http://stumbleupon.com/syndicate.php?stumbler=nog';
$rss = fetch_rss($url);

A loop in the php is used to contain the code to display the data parsed from the RSS to the file for each entry.

Changing num_items will alter the number of items displayed – the full RSS data is always downloaded though.

$num_items = 5;
$items = array_slice($rss->items, 0, $num_items);
foreach ( $items as $item ) {...........

Variables named $page, $url and $desc can give the on-screen text, the link url and description from data parsed from the RSS.

After adding in some PHP which basically echoes the returned values into HTML, the final block of php for my movable type index looked similar to what follows:

<?php
require_once '/home/nogwa/etc/magpierss/rss_fetch.inc';
$url = 'http://stumbleupon.com/syndicate.php?stumbler=nog&comments=1';
$rss = fetch_rss($url);
echo "<div><div class='sidetitle'>My recent 
<a href='http://www.stumbleupon.com/'>
<img src='http://stumbleupon.com/images/getstumbleupon.gif' alt='stumble toolbar' />
</a> sites</div><ul>"; 
$num_items = 5;
$items = array_slice($rss->items, 0, $num_items);
foreach ( $items as $item ) {
  $page = htmlspecialchars ($item[title], ENT_QUOTES,"utf-8");
  $url = htmlspecialchars ($item[link], ENT_QUOTES,"utf-8");
  $desc = $item[description];
  echo "<li><a href='",$url,"' title='Another Great Stumble'>
  $page</a><br /><span>$desc</span></li>";
}
echo "</ul></div>";
?>

If you wanted a list of the recently thumbed sites you would use the feed url without the &comments=1 part. Whereas if you wanted to list the sites that are on the StumbleUpon blog – but not display the comments you would keep the &comments=1 in the feed but omit the echo of the $desc variable.

There are other configuration options – for instance magpie caches the list it retrieves only checking and refreshing once an hour.

The caching parameters can be changed, but I tend to think an hour is an OK balance for keeping reasonably recent stumbles displayed and not making unnecessary requests to the RSS feed servers.

The results – though now using del.icio.us and a couple of BBC news rss feeds can be seen on the top of my home page

UPDATE

It’s no longer just RSS feeds -the latest versions of MagpieRSS allows the same approach to be used for just about any feed (including atom).

And finally – a demonstration of the whole thing working. Here is what the above script would currently output from my Stumbleupon RSS feed (It should be the same as what is posted on my Stumbleupon page):

My recent stumble toolbar sites