Using Flickr API to Get Photo Information

A simple PHP script to get a list of a user’s uploaded photos through the Flickr REST API. For each photo, the script gets its title, description, original photo URL and geolocation.

To use the Flickr API, an API Key (Consumer Key and Consumer Secret) and an OAuth Token (and OAuth Token Secret) are needed. An API Key is obtained by registering a new application (http://www.flickr.com/services/apps/create/apply/), and an OAuth Token is obtained by authenticating the user and granting access to the application (http://www.flickr.com/services/api/auth.oauth.html).

PHP Code

<?php
$page = 1;
$hasMorePhotos = true;
while ($hasMorePhotos) {
    $timestamp = time();

    $url = 'http://ycpi.api.flickr.com/services/rest/';
    $parameters
            = 'extras=description%2Cgeo%2Coriginal_format'
                    . '&format=json'
                    . '&method=flickr.people.getPhotos'
                    . '&nojsoncallback=1'
                    . '&oauth_consumer_key=<consumer_key>'
                    . '&oauth_nonce=0'
                    . '&oauth_signature_method=HMAC-SHA1'
                    . '&oauth_timestamp=' . ((string) $timestamp)
                    . '&oauth_token=<oauth_token>'
                    . '&oauth_version=1.0'
                    . '&page=' . ((string) $page)
                    . '&per_page=500'
                    . '&user_id=me';

    $signatureText = 'GET&' . rawurlencode($url) . '&' . rawurlencode($parameters);
    $signatureKey = '<consumer_secret>&<oauth_token_secret>';
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $signatureText, $signatureKey, true)));

    $parameters .= ('&oauth_signature=' . $signature);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_URL, ($url . '?' . $parameters));
    $result = curl_exec($curl);

    $resultJson = json_decode($result, true);

    if (count($resultJson['photos']['photo']) > 0) {    
        foreach ($resultJson['photos']['photo'] as $photo) {
            echo 'ID: ' . $photo['id'] . "\n";
            echo '    TITLE: ' . $photo['title'] . "\n";
            echo '    DESCRIPTION: ' . $photo['description']['_content'] . "\n";
            echo '    URL: ' 
                    . 'http://farm' . $photo['farm'] . '.staticflickr.com/'
                    . $photo['server'] . '/' 
                    . $photo['id'] . '_' . $photo['originalsecret'] . '_o.' . $photo['originalformat'] . "\n";
            echo '    GEOLOCATION: ' . $photo['latitude'] . ', ' . $photo['longitude'] . "\n";
        }
        $page++;
    } else {
        $hasMorePhotos = false;
    }
}
?>

where <consumer_key>, <consumer_secret>, <oauth_token> and <oauth_token_secret> are obtained through the application registration and user authentication process.

Explanations

while ($hasMorePhotos) {
...
}

Each of API call returns a “page” of photo information, which is a list of at most 500 photos. Hence, a loop is needed to retrieve all pages.

    $url = 'http://ycpi.api.flickr.com/services/rest/';

The URL for the API call.

    $parameters
            = 'extras=description%2Cgeo%2Coriginal_format'
                    . '&format=json'
                    . '&method=flickr.people.getPhotos'
                    . '&nojsoncallback=1'
                    . '&oauth_consumer_key=<consumer_key>'
                    . '&oauth_nonce=0'
                    . '&oauth_signature_method=HMAC-SHA1'
                    . '&oauth_timestamp=' . ((string) $timestamp)
                    . '&oauth_token=<oauth_token>'
                    . '&oauth_version=1.0'
                    . '&page=' . ((string) $page)
                    . '&per_page=500'
                    . '&user_id=me';

Form the parameters needed for the API call.

    $signatureText = 'GET&' . rawurlencode($url) . '&' . rawurlencode($parameters);
    $signatureKey = '<consumer_secret>&<oauth_token_secret>';
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $signatureText, $signatureKey, true)));

The OAuth Signature is generated by hashing the URL and parameters (in lexicographical order), using the Consumer Secret and OAuth Token Secret as hash keys.

    $parameters .= ('&oauth_signature=' . $signature);

Append the generated signature to the request parameters.

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_URL, ($url . '?' . $parameters));
    $result = curl_exec($curl);

Use Curl to make the API request and get the response.

    $resultJson = json_decode($result, true);

Parse the JSON response.

        foreach ($resultJson['photos']['photo'] as $photo) {
            echo 'ID: ' . $photo['id'] . "\n";
            echo '    TITLE: ' . $photo['title'] . "\n";
            echo '    DESCRIPTION: ' . $photo['description']['_content'] . "\n";
            echo '    URL: ' 
                    . 'http://farm' . $photo['farm'] . '.staticflickr.com/'
                    . $photo['server'] . '/' 
                    . $photo['id'] . '_' . $photo['originalsecret'] . '_o.' . $photo['originalformat'] . "\n";
            echo '    GEOLOCATION: ' . $photo['latitude'] . ', ' . $photo['longitude'] . "\n";
        }

Iterate through and print out the returned photo information.

Note: php and php-curl are needed to run the script, and they may not  be present in a default installation. They can be installed through the package manager

References

[1] http://www.flickr.com/services/apps/create/apply/
[2] http://www.flickr.com/services/api/auth.oauth.html
[3] http://www.flickr.com/services/api/flickr.people.getPhotos.html

Advertisements

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s