Decode json string returned from Flickr API using PHP, curl
Asked Answered
T

5

15

Im trying to decode a json string returned from flickr within my PHP code. Im using CURL but it keeps returning a string even when I wrap json_decode() around the json sring variable. Any ideas?

$api_key = '####';
$photoset_id = '###';

$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json&jsoncallback=1';

$ch = curl_init(); // open curl session

// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session
var_dump(json_decode($data));
Turtle answered 2/5, 2010 at 4:23 Comment(1)
What dos the response look like?Casuist
P
18

That's because the returned data is not valid JSON. Its valid JavaScript, though. The returned data is wrapped inside a default callback function called jsonFlickrApi.

You need to get rid of the JSON callback which wraps the JSON inside a callback function which is then supposed to be executed on the client side. You need to do some string manipulation on the returned JSON to remove the default callback jsonFlickrApi and then pass it to json_decode

$api_key = '####';
$photoset_id = '###';

$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json';

$ch = curl_init(); // open curl session

// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session

$data = str_replace( 'jsonFlickrApi(', '', $data );
$data = substr( $data, 0, strlen( $data ) - 1 ); //strip out last paren

$object = json_decode( $data ); // stdClass object

var_dump( $object );
Prague answered 2/5, 2010 at 4:27 Comment(1)
See the answer below for a simpler alternative (note to others as I almost missed it myself).Conflict
A
48

Your request URL ends with:

&format=json&jsoncallback=1';

The correct name of the parameter is nojsoncallback, so the right URL you should be using ends like this:

&format=json&nojsoncallback=1';

Change that and it should work.

Regards.

Abdominous answered 9/5, 2010 at 0:26 Comment(2)
@xmarcos - Good one! Wish @Globalz* had chosen this as the correct answer because I almost missed it.Tincture
Well, you still need a little string manipulation to adjust the "\'" characters in the JSON (in titles and comments) to be plain single quotes instead... but great answer.Orthogonal
P
18

That's because the returned data is not valid JSON. Its valid JavaScript, though. The returned data is wrapped inside a default callback function called jsonFlickrApi.

You need to get rid of the JSON callback which wraps the JSON inside a callback function which is then supposed to be executed on the client side. You need to do some string manipulation on the returned JSON to remove the default callback jsonFlickrApi and then pass it to json_decode

$api_key = '####';
$photoset_id = '###';

$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=json';

$ch = curl_init(); // open curl session

// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session

$data = str_replace( 'jsonFlickrApi(', '', $data );
$data = substr( $data, 0, strlen( $data ) - 1 ); //strip out last paren

$object = json_decode( $data ); // stdClass object

var_dump( $object );
Prague answered 2/5, 2010 at 4:27 Comment(1)
See the answer below for a simpler alternative (note to others as I almost missed it myself).Conflict
O
2

Even better instead of using a format=json in your url, use format=php_serial and get a serialize string then you wont have to worry about valid formating from flickr and you get an array in return

$api_key = '####';
$photoset_id = '###';

$query = 'http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key='.$api_key.'&photoset_id='.$photoset_id.'&extras=url_o,url_t&format=php_serial';

$ch = curl_init(); // open curl session

// set curl options
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
$data = curl_exec($ch); // execute curl session
curl_close($ch); // close curl session

$output = unserialize ($data);
Out answered 16/7, 2010 at 15:59 Comment(0)
F
0

stack overflow saves the day again. I scoured the flickr documentation and found NO MENTION of this "nojsoncallback" paramater.

who makes such a feature by default, then doesn't tell anyone how to disable it?

even worse, why would it be written that you have to ENable it in order to DISable the function?!

ridiculous... but thanks for the heads up, this fixed my problem!

Fritz answered 2/6, 2010 at 15:47 Comment(0)
L
0

The details of nojsoncallback is at the bottom this page https://www.flickr.com/services/api/response.json.html

Layer answered 11/10, 2014 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.