Search code examples
phpapicurlflickr

Flickr photo search API not returning anything


I tried to do a flickr search using cURL and Flickr API. When trying to print the response, it prints "1". What is wrong with my code?

$params = array(
    'api_key'   => 'b838e46f6e8eada6a62fac7e2b25ffcc',
    'method'    => 'flickr.photos.search',
    'format'    => 'php_serial',
    'text'  =>'cars'
);

$encoded_params = array();

foreach($params as $k => $v){
    $encoded_params[] = urlencode($k).'='.urlencode($v);
}

$ch = curl_init();
$timeout = 0; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'https://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);
print_r($file_contents);
curl_close($ch);

$rsp_obj = unserialize($file_contents);

//echo 'https://api.flickr.com/services/rest/?'.implode('&', $encoded_params);
echo print_r($rsp_obj);

Solution

  • If cURL can not verify the certificate of the remote site, then you can set the option CURLOPT_SSL_VERIFYPEER to false, so that it will not try to do that.

    This has of course some security implications – the remote site might not be who they pretend they are, but if you’re only doing a search that’s a rather minor concern, especially when you’re only testing locally for now. For a production app on a server you should maybe look into getting that fixed though, especially when you’ll be doing something that isn’t a pure search later on.