Search code examples
phpcurlflickr

Error using Flickr photos search api: Invalid API Key (Key has invalid format)


In my app I'm doing some work to construct a query string to call through Flickr API, the result is something like this:

https://api.flickr.com/services/rest/?format=json&api_key={MY API KEY}&method=flickr.photos.search&tags=happy%2Cdog

Requesting that in the browser works just fine, however, requesting the same query with curl returns an error:

jsonFlickrApi({"stat":"fail","code":100,"message":"Invalid API Key (Key has invalid format)"})

Here's my curl code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $this->query);
$result = curl_exec($ch);
curl_close($ch);

Solution

  • If it works when the URL generated by your system gets copy&pasted into the browser, then something must be different.

    Your debug output likely got interpreted as HTML, so if there is an erroneous & in there somewhere instead of just an &, it still looks like an &, and also gets copy&pasted as &.

    htmlspecialchars is always helpful in cases like this, to see the actual value, and not what the browser has interpreted it as.