Currently browsing rss


Flickr Namespace and array_multisort() Issues

Fri, 15 Dec 2006 18:15 UTC

Aaron Wormus recently criticized me on IRC for putting too much thought and effort into blogging. He may have a point there. I’ve got a huge list of things I want to write about, but I haven’t put forth the effort because I do take too much time to write a post. So, here goes one for writing up a quick post . . .

I recently upgraded a few things around here (in particular, an upgrade to PHP 5.2), and I noticed two issues that occurred after the upgrade:

  1. My Flickr photo feed no longer worked, and
  1. My blogroll was no longer sorting alphabetically

    I easily solved both of these issues:

    The Flickr photo feed

    I’m not entirely sure whether this problem was a result of the PHP 5.2 upgrade and SimpleXML becoming more particular about namespaces or Flickr simply changing the namespace string in their feed. I believe it was the latter. In short, my code stopped working because the namespace lacked a trailing slash (/). It would’ve been great if Flickr had notified everyone about this first.

    The code looked something like this:

    <?php
     
    $flickr_rss = simplexml_load_file('http://www.flickr.com/services/feeds/photos_public.gne?id=49198866@N00&format=rss_200');
     
    foreach ($flickr_rss->channel->item as $item)
    {
        $link   = (string) $item->link;
        $media  = $item->children('http://search.yahoo.com/mrss');
        $thumb  = $media->thumbnail->attributes();
        $url    = (string) $thumb['url'];
        $width  = (string) $thumb['width'];
        $height = (string) $thumb['height'];
        $title  = (string) $item->title;
     
        // display image here
    }
     
    ?>

    Note the namespace listed in $item->children(‘http://search.yahoo.com/mrss’);. It’s lacking a trailing slash. This was never a problem before, but it seems that the Flickr feed changed to include a trailing slash, so SimpleXML was no longer able to properly get the data. I simply changed it to http://search.yahoo.com/mrss/ (note the trailing slash), and all worked fine.

    Blogroll sorting

    This was more than likely a result of the upgrade to PHP 5.2, though I can’t be sure. My blog sorting code worked like this:

    <?php
     
    // sort by name
    $name = array();
    foreach ($blogs as $k => $v)
    {
        $name[$k] = $v['name'];
    }
    array_multisort($name, SORT_ASC, $blogs);
     
    ?>

    Unfortunately, after the upgrade, this no longer worked. I had to add the SORT_STRING flag as a parameter to array_multisort() so that the line now reads:

    <?php
     
    array_multisort($name, SORT_ASC, SORT_STRING, $blogs);
     
    ?>

    And that fixed it.

    Just thought I’d share the info in case this helps anyone.

    Comments 2 Comments »  Permalink Permalink  Tags Tags: , , , ,


Share Your OPML

Mon, 8 May 2006 14:35 UTC

It seems I’ve been focused on OPML for the past few posts, and why not? OPML appears to be gaining a lot of attention lately. There’s even an OPML Camp in Boston later this month.

So, what’s all the fuss about? Adam Green summarizes his vision for the future of OPML on the OPML Camp blog:

I see OPML as an extremely flexible container for many types of XML and HTML data. Its simplicity and extensibility means that it can be used to bring together many types of structured content that are currently being developed on the Web, such as microcontent, attention data, identity data, and data that will eventually comprise the Semantic Web. OPML won’t replace these many efforts, it will help integrate them, by providing a common packaging mechanism.
Adam Green, OPML Camp blog

As of right now, many sites are using RSS as a common content delivery format. While RSS works for syndication, though, I think that it lacks the extensibility and context to fill the role of this “flexible container.” OPML won’t replace RSS, but it will aid in its delivery. So, I’m a little more than casually interested in seeing the application and future direction of OPML.

Along these lines, Dave Winer, launched today his new Share Your OPML service, the purpose of which is to “gather a community of subscription lists, in OPML format, and aggregate them in interesting ways.” It’s unclear to me (right now) the practical value of this service, since I was unable to discover how to actually generate OPML from the site. If the site were to provide OPML feeds for each data display, then its value would be readily apparent. Right now, I can only find an OPML feed for the Top 100 shared feeds.

Nevertheless, in the spirit of signing up for every new service out there, you can view my shared feeds here.

Comments No Comments  Permalink Permalink  Tags Tags: , , , ,


Generating OPML From del.icio.us (And Getting All Your Links)

Sat, 6 May 2006 15:40 UTC

UPDATE: Please note that del.icio.us has updated their API in the time since I wrote this. I have updated the URLs in my examples accordingly. The authentication is much safer since they are now using SSL.

The other day, I mentioned that I generate my blogroll from an OPML file. Until recently, this OPML file was generated manually by me whenever I updated my blogroll in NewsFire.

Now, there are some problems with NewsFire, one of which is that you cannot generate OPML for a specific group or selection of blogs (NetNewsWire excels in this area, however). Instead, the OPML file generated is a dump of all your feeds. This was not ideal for me, since I then had to go through and clean up the OPML before uploading it to my site. Needless to say, I updated my OPML as little as possible.

After my last blog post, I posted a comment to Chris’s blog saying that he should do something similar. That is, I suggested he generate his blogroll from an OPML file instead of del.icio.us, which has the unfortunate limitation of only 30 items per RSS feed. He replied, saying that he would then have to update two places (his feed reader and del.icio.us) instead of only one. In short, it would create more work. After assessing my practices, I cannot agree more.

So, I investigated a way to get around the 30-feed limitation in del.icio.us, and I found that their API allows you to do just this, albeit with a few restrictions of its own, which I’ll explain in a few moments. Using the del.icio.us API, instead of their RSS feeds, I was able to use the following code to first check whether my del.icio.us account has been updated since I last cached their data, and, if so, to grab all of the links for a particular tag and cache the data to a file for later use:

<?php
$username   = 'your_username';
$password   = 'your_password';
$cache_file = '/tmp/blogroll.xml';
 
// check for updates to del.icio.us account
$update = simplexml_load_file("https://{$username}:{$password}@api.del.icio.us/v1/posts/update");
 
if (strtotime($update['time']) > filemtime($cache_file))
{
    // del.icio.us has been updated since last cache; recache
    $data = file_get_contents("https://{$username}:{$password}@api.del.icio.us/v1/posts/all?tag=blogroll");
    file_put_contents($cache_file, $data);
}
 
// read links from cached del.icio.us data
$blogroll = simplexml_load_file($cache_file);
 
foreach ($blogroll->post as $blog)
{
    echo '<a href="' . htmlentities($blog['href']) . '">';
    echo htmlentities($blog['description']);
    echo "</a><br />\n";
}
?>

This code sample uses SimpleXML to traverse the nodes of the XML documents retrieved from del.icio.us. It first polls del.icio.us with a request to see the update timestamp. Checking this before executing a query to see all posts saves bandwidth for del.icio.us and can help us determine whether our cached file needs updating. This is also the recommended practice (from del.icio.us).

This approach can help you grab all of your links (or all links for a specific tag) from del.icio.us. I’ve noted several issues, however:

  • You can specify a tag to the /posts/all command with ?tag=tagname, but you cannot specify a combination of tags to retrieve
  • You cannot specify a tag to the /posts/update command, so the update timestamp received is for your last overall update to del.icio.us—if specifying a tag to /posts/all, then it would be beneficial to specify a tag to this command, saving del.icio.us even more bandwidth
  • This method requires HTTP authentication; you’ll notice how I passed a username and password through the URL to retrieve the data—you can retrieve only your links; whereas, using RSS, you may retrieve anyone’s links, but you are limited to their 30 most recent links

    Finally, I have implemented this approach using my blogroll tag on del.icio.us. My OPML file is now generated nightly (if detecting updates to del.icio.us). This will end up saving me quite a bit of time, since I can use the del.icio.us plugin for Firefox to add any site I visit to my blogroll, and, then, I can import the generated OPML into my feed reader at my leisure, with little to no headache.

    You can see my script, which is executed by a nightly cron job, here. You may notice that it’s using a getRSSLocation() function to auto-discover the RSS feed from each site for inclusion in the OPML file. I grabbed this function from Keith Deven’s site, and you can see my implementation of it here.

    Comments 4 Comments »  Permalink Permalink  Tags Tags: , , , , ,