Search for photos using PHP and the flickr API : Simple way to search Flickr API using PHP

Flickr provides a large number of API methods which you can use to search for images, contacts, comments, notes etc.
This post will show the use of one simple method to search images using PHP. We will send a REST request and will ask for a JSON response. Though this method allows you to search using tags, text, date and others parameters, we will restrict to the use of only tags in this example. You are free to add other paramters.

First of all you will need an api_key which is mandatory to use with flickr api. Go to and get one. We are ready to jump into code now.

First define the search parameters and create the URL to which the request will be sent.

$api_key = "YOUR_API_KEY_HERE";

$tag = 'flower,bird';
$perPage = 25;
$url = '';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';

Assign your api key to variable $apiKey. $tag is the tag name you want to search for. You can enter more then 1 tag separated by commas. $perPage is the number of results in a single response.
Assign these variable to corresponding keys in URL and set the format to json. nojsoncallback = 1 will give us raw json instead of wrapping it up in a callback function.

Now try to get the results from flickr. Once response JSON is received, use json_decode to convert it to an object so that we can iterate on it and get values.

$response = json_decode(file_get_contents($url));

Finally, check the status from response. If it is ok, we can then create the image URLs for each picture. You will see that there is no image path in response. Instead there are some values for farm, server,id,secret etc. An image on flickr has the following URL structure for an image.

Farm-id, server id, image-id and secret are avilable in response itself. Size can be one of the following characters

  • s : Small
  • t : Thumbnail
  • – : Medium
  • b : Large
  • o :Original

Using the sizes mentioned above, we have created 2 URLs for each image. First is small that is displayed on the page and the second one opens a large version of same image in a new window.

if($response->stat == 'ok')

    $photos = $response->photos->photo;
    if(count($photos) > 0)
        echo '<ul>';
    foreach($photos as $photo)
        $farmId = $photo->farm;
        $serverId = $photo->server;
        $id = $photo->id;
        $secret = $photo->secret;
        $title = $photo->title;
        $imagePathThumbnail = 'http://farm'.$farmId.''.$serverId.'/'.$id.'_'.$secret.'_s.jpg';
        $imagePathLarge = 'http://farm'.$farmId.''.$serverId.'/'.$id.'_'.$secret.'_b.jpg';
        $image = '<li>';
        $image.= '<a href="'.$imagePathLarge.'" target="_blank">';
        $image.= '<img src="'.$imagePathThumbnail.'" alt="'.$title.'">';
        $image.= '</a>';
        $image.= '</li>';
        echo $image;
    echo '</ul>';
        echo 'No Results';
    echo '<strong>Error : </strong>'.$response->message;
You can read more about the search method at

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 )

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