Rio Bautista’s TechLog

Tech solutions worth remembering

Integrating Flickr into my PHP web-app

I’m now to grab some photos from Flickr. Photos tagged with a given string and link to those images from my site.

I tried checking the Flickr API documentation but all it shows there are the available methods and their documentation. But with some poking around I finally found this page (http://www.flickr.com/services/api/auth.howto.web.html) which gives me the “how to’s” to get me started.

We’ll now need to download an API from the list. I initially picked-out the PHP5 API but after going through the doc, it turns out we need to make sure the CURL and SimpleXML extensions should be enabled. I’m not sure if that’s available on the target server so I’m going for the phpFlickr library for PHP4. It comes with some PEAR files but doesn’t really require you to have PEAR running on your server.

Below is a simple program to search for pictures having a particular tag (as I’m supposed to do with my site) :


<?
require_once(“phpFlickr.php”);
$f = new phpFlickr(“yourAPIkey”);
$result = $f->photos_search(array(‘tags’=>’rio’));
$photo = $result['photo'][0];
$info = $f->photos_getInfo($photo['id']);
print_r($info);
?>

What I’ll try to do now is to display the picture on my site. This is not on the standard documentation and from searching the internet I came across this solution. You should be able to access the picture from the Flickr site using a URL like this:


http://farm{farm_id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
or

http://farm{farm_id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg

You’ll be able to get the farm_id, server-id, id and secret by executing the photos_getInfo which returns an array containing all info about about given picture_id. Using the code above we can add this line to display an image from Flickr:

echo “<img src=’http://farm{$info['farm']}.static.flickr.com/{$info['server']}/{$photo['id']}_{$info['secret']}_m.jpg’>”;

You should now be able to display a picture from Flickr. Have fun…

January 10, 2008 - Posted by Rio | Flickr API, php | | No Comments Yet

No comments yet.

Leave a comment