Learning Outcomes: Interacting with the Flickr API using PHP

Learning Outcomes: Interacting with the Flickr API using PHP

The Flickr API supports several request and response formats and allows access to nearly all of the functionality of the web interface. REST and Serialized PHP were the most straightforward formats for our purposes. The REST format allows us to call API methods to request photos and associated meta data using simple HTTP GET actions. The responses come back in serialized PHP format which you can convert into an array using PHP’s unserialize() function.

Before you can access the Flickr API, you’ll need to apply for an API key.

The first step in making use of Flickr’s services is to write a function that will allow you to easily make calls to API methods. The function request() will take an array of parameters specifying the API method to be called and it’s required arguments. It will return an unserialized array containing the response.

function request($params){
    $api_key = '81e7e11aeac57ac668609d316f6388ce';
    $url = 'http://api.flickr.com/services/rest/?' .
           'api_key=' . $api_key . '&format=php_serial';
 
    foreach($params as $key => $val){
        $url .= '&' . urlencode($key) . '=' . urlencode($val);
    }
 
    $resp = file_get_contents($url);
    return  unserialize($resp);
}

All you need to do to make a request is format the $params array and pass it to request().
To get the user id for a Flickr user, call the flickr.people.findByUsername method:

$params = array('method' => 'flickr.people.findByUsername',
                'username' => 'hs0j');
$user = request($params);
$print_r($user);

$user contains the response from Flickr.It contains the following data:

Array
(
    [user] => Array
        (
            [id] => '30180364@N06'
            [nsid] => ' 30180364@N06'
            [username] => Array
                (
                    [_content] => 'hs0j'
                )
        )
    [stat] => 'ok'
)

From here, you can get a list of photos for the user by calling flickr.people.getPublicPhotos:

$params = array('method' => 'flickr.people.getPublicPhotos',
                    'user_id' => $user['id'],
                    'per_page' => 20);
$photos = request($params);

Now that you have a list of photos, you can iterate through the elements of $photos['photo'] and build URLs for each photo using build_photo_url(). For information regarding the photo URL format, see Photo Source URLs.

function build_photo_url($p){
     return 'http://farm' . $p['farm'] . '.static.flickr.com/' . $p['server'] .
            '/' . $p['id'] . '_' . $p['secret'] . '.jpg';
}
 
foreach ($photos['photo'] as $p){
    echo '<img src="' . build_photo_url($p) . '" alt="" />';
}

Flickr allows you to specify a number of image sizes when building photo URLs, making it easy to create a gallery of thumbnails that link to either a greybox or full size photo page. These sizes are explained in detail on the Photo Source URLs page.

The code here can be expanded upon to support all of the other methods that are detailed in the API Documentation. Adding support for the other methods you find useful and writing helper functions that tie several calls together will leave you with a very functional and reusable Flickr class.

Tags: , , , , , ,

Josh Yaganeh

I'm currently a senior studying Computer Science at the University of Oregon. I've been a student web developer at EMU Marketing since summer of 2008.

2 Comments Leave yours

  1. Nicely done Josh! I may have to mess around with your plugin sometime. I didn’t realize it was so easy to make REST calls to the flickr API. Otherwise I probably could have avoided using any of the pre-built PHP flickr libraries and just rolled my own.

  2. Hi this is exactly what I was looking for… a quick and simple tutorial illustrating how to pull photos from a flickr account. It seems as though the code in your pre tags is getting a little too escaped.

1 Trackbacks

Leave a Reply