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.


2 Responses to “Flickr Namespace and array_multisort() Issues”

Many Thanks to that info. We’re going to migrate our servers to 5.2 in the next days and I’d used array_multisort a few times in a project. So now I already know a point, which won’t work any longer.

Comment by Daniel
Sat, 16 Dec 2006 at 10:03 UTC | Permalink

Hey, this might be a couple of years old, but I found that snippet really useful for an xml feed that needed sorting :) Thanks! Never could get my head round multi-dimensional arrays, there’s just something about them that makes my brain burn out.

Comment by markowe
Sat, 1 Nov 2008 at 17:46 UTC | Permalink

Leave a Comment

XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
APPROVAL: Comments may require approval. Please be patient.