Connecting to the Flickr API with PHP

Asif Ahmed

Today, we will be covering how to connect to the Flickr API with PHP. This will a quick tutorial and we will show you the full code.

First we need to sign up for a Flickr API key…I know it’s annoying but that’s just how they roll. To sign up follow this link Once you have an API key you are good to go. For our example we will connecting to Flickr and getting a few publicly available pictures of birds and flowers. We will be using the Flickr photo search method specifically. To read more about how this service works and what parameters it uses go to its page on the Flickr site.

Alright so we have a key, and have read a little about how the API works. Let’s get some pictures!


$api_key = 'YOUR API KEY';

$tag = 'flower,bird,peacock';
$perPage = 25;
$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';

You will have to set your API key. The tag parameter we have set is to search for all pictures with a tag of flower, bird, or peacock. We only want 25 per page (it doesn’t matter, we only want the first page). We then start to create the URL we will be hitting to get results. We want to set the proper method (flickr.photos.search), the proper API key and the correct tags. We also want the format to be in JSON because JSON is lightweight and easy to parse with PHP (or anything really).


$response = json_decode(file_get_contents($url));
$photo_array = $response->photos->photo;

Now we will hit the URL with the PHP function file_get_contents and then we will decode it into a PHP object. The response has a lot of other information, but we only want the photo information object so we grab that. If you want to view the full contents of the response, you could do something like –

print_r($response)

You would need to see how the response is structured to see how you would parse it! Next we grab the relevant information from each photo and print it out. If you look at the $photo_array object that you received, it makes no sense. It has all sorts of parameters, like “farm”, “secret”, and “server”. To create the correct photo URL you will have to create the correct image URL (this is done probably to stop bots from stealing their photos). To figure out how to create these URL’s you can visit the Flickr documentation. Below is the code of how to go through the photo array and get the relevant information to create the URL. We will also end up printing out the image to the screen.


foreach($photo_array as $single_photo){

$farm_id = $single_photo->farm;
$server_id = $single_photo->server;
$photo_id = $single_photo->id;
$secret_id = $single_photo->secret;
$size = 'm';

$title = $single_photo->title;

$photo_url = 'https://farm'.$farm_id.'.staticflickr.com/'.$server_id.'/'.$photo_id.'_'.$secret_id.'_'.$size.'.'.'jpg';

print "<img title='".$title."' src='".$photo_url."' />";

}

We set the size of the image to be “m” which means medium (240 pixels on the longest size according to their documentation).

If you want to see this Flickr photo viewing script in action – click me. Also the entire code is reproduced below


<?php

$api_key = 'YOUR API KEY';

$tag = 'flower,bird,peacock';
$perPage = 25;
$url = 'http://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';

$response = json_decode(file_get_contents($url));
$photo_array = $response->photos->photo;

// print ("<pre>");
// print_r($response);
// print ("</pre>");

foreach($photo_array as $single_photo){

$farm_id = $single_photo->farm;
$server_id = $single_photo->server;
$photo_id = $single_photo->id;
$secret_id = $single_photo->secret;
$size = 'm';

$title = $single_photo->title;

$photo_url = 'http://farm'.$farm_id.'.staticflickr.com/'.$server_id.'/'.$photo_id.'_'.$secret_id.'_'.$size.'.'.'jpg';

print "<img title='".$title."' src='".$photo_url."' />";

}

?>

  • http://reportsfunda.com/ Vijay

    This is really awesome. Thanks a lot.

  • http://jackylee.com Jacky

    i play around the code u provided……failed

    finally i found that :
    $url = ‘http://api.flickr.com/services/……
    should change to
    $url = ‘https://api.flickr.com/services/……

    • Asif Ahmed

      Hi Jacky,

      I think Flickr has changed their API to only work over HTTPS recently. I will update this in a couple weeks.

    • http://www.trendingus.com Suraj Lulla

      Change it to $url = ‘https://api.flickr.com/services/rest/?method=flickr.photos.search';

  • http://www.clockregister.org MarctK

    really very useful. thank you.

  • http://www.moullablad.com Moullablad ltd

    hello i tested your code its working well :) thanks.

  • http://www.moullablad.com Moullablad ltd

    hello again concerning size you need just to remove _m from the link and you will have the original size of file i tested that its working fine.

    https://farm4.staticflickr.com/3929/15236018737_e6e6270629_m.jpg

    change to

    https://farm4.staticflickr.com/3929/15236018737_e6e6270629.jpg

  • http://www.sibertama.com Digital Agency Jakarta

    Great!! This is work..
    But how to upload image to flickr??
    Do you have another some cool stuf?

    • http://www.trendingus.com Suraj Lulla

      login to your flickr account and upload image with the tag used in your code

  • mightycpa

    What I found was that if you upload and tag a photo by email, then the API will identify the photo, but won’t return its info in the array:

    stdClass Object ( [photos] => stdClass Object ( [page] => 1 [pages] => 1 [perpage] => 25 [total] => 1 [photo] => Array ( ) ) [stat] => ok )

    See how the total = 1 but the array is empty?

    What a terrible API!

    It looks like I need to find another provider. But thanks for the code, because it allowed me to find this deficiency fairly quickly. I appreciate the fruit of your efforts.

  • thuy

    thanks you very much. I feel very happy when visit your site. thanks again. :D

  • njoku

    what is the file saved as?

    • http://www.trendingus.com Suraj Lulla

      The extension will be .php

  • njoku

    im a newbie

  • Mike Sinkula

    Thanks! I finally got my flicker functions running because of this article.