Search code examples
phpapiflickr

Flickr API Search?


I have some php code that retrieves a set of images based of a photoset. I want to manipulate this code so that it grabs a set of pictures from a search term.

I know you can use the method inbuilt into the api: flickr.photos.search

Just wondering how i can apply it to this code

 <?php
  $params = array(
'api_key'   => '2292bec0973f91d9f62fb606f85ee031',
'method'    => 'flickr.photosets.getPhotos',
'photoset_id'   => '72157622566216264',
'extras'    => 'original_format',
'format'    => 'php_serial'
  );
  $encoded_params = array();
  foreach ($params as $k => $v){ $encoded_params[] = urlencode($k).'='.urlencode($v); }

   $ch = curl_init();
   $timeout = 5; // set to zero for no timeout
   curl_setopt ($ch, CURLOPT_URL, 'http://api.flickr.com/services/rest/?'.implode('&',             $encoded_params));
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   $file_contents = curl_exec($ch);
   curl_close($ch);

   $rsp_obj = unserialize($file_contents);

   if ($rsp_obj['stat'] == 'ok') {

    $photos = $rsp_obj["photoset"]["photo"];

echo "
     <ul>";

foreach($photos as $photo) {

           $farm              = $photo['farm'];
           $server            = $photo['server'];
           $photo_id          = $photo['id'];
           $secret            = $photo['secret'];
           $photo_title       = $photo['title'];

         echo '<li><img          src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>';

     }
       echo "
           </ul>

            ";

} else {
        echo "Error getting photos";
   }
   ?>

Solution

  • Did you read the documentation? flickr.photos.search

    These methods return similar data, but the name of the top-level array is different (photos vs. photoset). Assuming you modified your parameters to use the correct method and pass the appropriate arguments, the only other thing you need to change is this line:

    $photos = $rsp_obj["photos"]["photo"];