connecting to flickr using PHP and PECL oAuth extension

flickr, one of the world dominant in photo sharing service, has added support to oAuth recently. Though their old authentication system still works (marked as deprecated but not discontinued) I find it’s wise to use their oAuth system for the better future proof application development.

PHP has a nice extension to perform oAuth dance in it’s pecl repository – pecl oAuth. Here is the code to connect to flickr using this extension. The only catch I found and took me more than 30 minutes to figure out a failed attempt, is you will have to append the permission flag in it’s oAuth authorization url. Pass either one of these permission flags “read”,”write” or “delete” as “&perms=” (flickr.php, line # 20) and then the redirection will be successful. Register your Flickr application from here, you will find your consumer key and secret key in next page in the App Garden.

While creating new application in flickr app garden, point the callback url to your flickr.php – thats it.

source code of config.php


And here is the source code of flickr.php

 * flickr authentication script based on
 * pecl oauth extension
$oauthc = new OAuth($oauth['flickr']['consumerkey'],
if(empty($_SESSION['frequest_token_secret'])) {
    //get the request token and store it
    $request_token_info = $oauthc->getRequestToken($oauth['flickr']['requesttokenurl']); //get request token
    $_SESSION['frequest_token_secret'] = $request_token_info['oauth_token_secret'];
    header("Location: {$oauth['flickr']['authurl']}?oauth_token=".$request_token_info['oauth_token']."&perms=read");//forward user to authorize url with appropriate permission flag
else if(empty($_SESSION['faccess_oauth_token'])) {
    //get the access token - dont forget to save it 
    $request_token_secret = $_SESSION['frequest_token_secret'];
    $oauthc->setToken($_REQUEST['oauth_token'],$request_token_secret);//user allowed the app, so u
    $access_token_info = $oauthc->getAccessToken($oauth['flickr']['accesstokenurl']);
    $_SESSION['faccess_oauth_token']= $access_token_info['oauth_token'];
    $_SESSION['faccess_oauth_token_secret']= $access_token_info['oauth_token_secret'];
if(isset($_SESSION['faccess_oauth_token'])) {
    //now fetch current users profile
    $access_token = $_SESSION['faccess_oauth_token'];
    $access_token_secret =$_SESSION['faccess_oauth_token_secret'];
    $data = $oauthc->fetch('');
    $response_info = $oauthc->getLastResponse();
    echo "<pre>";
    echo "</pre>";

You can download the complete package from here (Thanks

Note: I have written a similar post to demonstrate connecting to twitter and linkedin via their oAuth protocol which you can find here :)

9 thoughts on “connecting to flickr using PHP and PECL oAuth extension”

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s