del.icio.us is the fabulous social bookmarking site which allows you to add tags and descriptions to your bookmarks, and share them with others. It provides an RSS feed for each user and each tag, and these RSS feeds allow you to mirror content from del.icio.us onto your website with ease. The MagpieRSS library provides a simple interface for parsing and displaying information from these feeds in PHP.
This hack is very similar to the ``Adding RSS Content to Your Homepage'' hack. The main difference is that we will create a display that is tailored specifically to del.icio.us RSS feeds, which provide several sets of additional information not provided by most RSS feeds.
First step to this hack is to download MagpieRSS from the MagpieRSS homepage, http://magpierss.sourceforge.net/. Once you've downloaded the latest release from Sourceforge, you will need to create a directory in your webroot to store the include files that MagpieRSS needs. The documentation suggests a ``magpierss'' directory.
Once you've created this directory, copy the four include files (rss_fetch.inc, rss_parser.inc, rss_cache.inc, and rss_utils.inc), and the directory extlib (which contains a modified version of the Snoopy HTTP client) into the directory.
That's it. You've installed the MagpieRSS library.
To parse del.icio.us RSS, we will use the fetch_rss method provided by MagpieRSS. First, we include the two include files we will need: one for parsing the RSS, the second for converting the date fields to a timestamp.
require_once('magpierss/rss_fetch.inc'); require_once('magpierss/rss_utils.inc');
Then, we fetch the RSS file:
$rss = fetch_rss("http://del.icio.us/rss/crschmidt");
Once we've done this, we can iterate over the entries, the same way we would with any other RSS feed. However, there are several additional items which are available in del.icio.us feeds which aren't available in most other RSS feeds:
Much of the rage behind del.icio.us has been its tagging system: allowing people to gather links with similar content by assigning the same subject to them. Using the dc:subject, we can show these links alongside our entries, so that people can browse to other links that might be considered similar.
To demonstrate this, we'll create a list of links from del.icio.us, each followed by a list of links to each tag, with the date each was posted.
foreach ($rss->items as $item) { $tags = explode(" ", $item['dc']['subject']); $time = parse_w3cdtf($item['dc']['date']); $date = date("Y-m-d", $time); echo "<li><a href='".$item['link']."'>".$item['title']."</a>, posted $date<br />" . ".$item['description']."<br />"; if (is_array($tags)) foreach($tags as $v) echo "<a href='http://del.icio.us/tag/$v'>$v</a> "; echo "</li>"; }
First, we split up the tags: the explode
call splits the subject up by spaces, creating an array of the tags used on the post. We then parse the date, and create a date string in the same format that del.icio.us itself uses. For more information on how to change the date format, refer to the documentation for PHP's date
function.
We then print out The link, title, date, and description. Finally, we iterate over the tags, making each tag a link to the del.icio.us page for that tag -- this allows people to find other similar links if they are interested.
As you can see, it is relatively simple to build up a clean display similar to del.icio.us's own display with minimal effort, based on the RSS feed provided by del.icio.us.