HOME  → POSTS  → 2004

Using Del.icio.us Feeds With SimplePie

Projects and Code504 words3 minutes to read

I know that a lot of people are using Magpie RSS to power their del.icio.us sidebar links, so I figured I’d throw out a code sample showing how you could use SimplePie for the same task. If you need to come to terms with why SimplePie might be a better choice of RSS parser, check out the SimplePie project page.

The only other thing that might be worth mentioning is that SimplePie still has a difficult time with non-encoded special characters in feeds and feeds in non-western-european/non-american/non-australian languages. Beyond that, it’s pretty solid so far.

Now, before I get to explaining, I’ll point to a del.icio.us demo with SimplePie in action. Now, here’s the code as a whole:

<?php
include_once("../simplepie.inc");

@$rss = simplepie("http://del.icio.us/rss/tag/web", false, true, 0.02);

if ($rss != false) {
    print("<p>Reading from " . get_feed_url() . "</p>");
    print("<ul>");

    for ($x=0; $x < 15; $x++) {
        print('<li><a href="' . get_item_permalink($x, $rss) . '">' . get_item_title($x, $rss) . '</a></li>');
    }

    print("</ul>");
}
else {
    print("The del.icio.us feed is not currently available.");
}

print("Powered by " . sp_linkback());

Let’s begin with the first part, include_once("../simplepie.inc");. This is an instance of including the SimplePie library. In my example, the simplepie.inc file is located in the subdirectory.

The next line is where we start processing the rss feed:

<?php
@$rss = simplepie("http://del.icio.us/rss/tag/web", false, true, 0.02);

We start by passing the first parameter, which tells SimplePie where the feed is that we want to process. The second parameter asks whether or not to enable a mode called XMLDump, which we don’t need for this excersize. The third parameter asks whether we want to cache the feed that we’re reading. Since SimplePie currently doesn’t work if the feed isn’t cached (unless it’s a local file), this needs to be true. Lastly, we tell SimplePie how many hours before refreshing the cached feed. We then set the feed to the $rss variable.

Next, we check whether the feed actually exists and was processed. If the expression if ($rss != false) (if $rss is not false…) is true, then we begin displaying the del.icio.us feed.

If you so choose, you can display the URL of the feed you’re using with the get_feed_url() function. I also chose to have this display as an unordered list, since that’s really what it is.

Inside of that <ul></ul> code block, I’ve set up a for-loop. Using $x as my counter variable, I decided to display only the most recent 15 entries. If you want to display all entries in the feed, you can use the get_item_quantity($rss) function.

As a backup plan, I like to add a short message for when the feed isn’t working. In my case, I said that “The del.icio.us feed is not currently available.” Simple and to-the-point.

Next, you want to tell everybody how you made all of this really cool stuff happen by linking back to the SimplePie project page. The easiest way is with the sp_linkback() function.

There! You should now have a fully-functional del.icio.us sidebar right there in your website.

Ryan Parman

is an engineering manager with over 20 years of experience across software development, site reliability engineering, and security. He is the creator of SimplePie and AWS SDK for PHP, patented multifactor-authentication-as-a-service at WePay, defined much of the CI/CD and SRE disciplines at McGraw-Hill Education, and came up with the idea of “serverless, event-driven, responsive functions in the cloud” while at Amazon Web Services in 2010. Ryan's aptly-named blog, , is where he writes about ideas longer than . Ambivert. Curious. Not a coffee drinker.