Mou

Flickr API PHP Wrapper

After a little prompting yesterday, I decided to spend a bit of time looking into the Flickr API and what I could do with it – up to now Ive been happy with pulling photos from the RSS feeds.

Without spending too much time looking, I came across a Flickr API PHP Wrapper made by none other than Rasmus Lerdorf using CURL. Given my usage, I didnt think there was any point “re-inventing the wheel” (so to speak) so it made sense to go with something pre-made – or at least close. Using this, pulling any number of pictures from my Flickr account is as easy to calling a function and initiating a loop.

One thing I couldnt find in his code was a simple way of calling back the link to the main Flickr page for each photo – as per their community guidelines. To get around this, Ive added the following function into the flickr class:

function getPhotoLinkURL($p) {
   return "http://flickr.com/chrismou/{$p['id']}\" title=\"{$p['title']}";
}

I’d also suggest adding alt text to the images – the simplest way to do this is to find the getPhotoURL function and replace it with this:

function getPhotoURL($p, $size='s', $ext='jpg') {
   return "http://static.flickr.com/{$p['server']}/{$p['id']}_{$p['secret']}_{$size}.{$ext}\" alt=\"{$p['title']}";
}

As an example, you can use Rasmus’s PHP wrapper with the new code to display a list of your 30 most recent photos (all linking back to the original images):

<?php
   include 'flickr_api.php';
   $flickr = new Flickr($secrets);
   $photos = $flickr->peopleGetPublicPhotos('11183962@N00',null,30);
   foreach($photos['photos'] as $photo) {
       $url = $flickr->getPhotoURL($photo);
       $linkBack = $flickr->getPhotoLinkURL($photo);
       echo '<a href="' . $linkBack . '" target="_blank">';
       echo '<img src="'.$url.'" />';
       echo '</a>';
    }
?>

What really interests me is how easy it is to upload/edit images using the wrapper – I’ve heard of other Flickr wrappers such as Phlickr, but I havent really had time to look at them yet. Theres a couple of flickr related tasks Ive wanted to do from my WordPress dashboard for a while and this may help bring them closer to reality. Possibly a future plugin release?


There are no responses yet

  1.  

Leave a Reply

css.php