使用 PHP、curl 解码从 Flickr API 返回的 json 字符串

发布于 2024-08-30 16:04:20 字数 652 浏览 3 评论 0原文

我试图在我的 PHP 代码中解码从 flickr 返回的 json 字符串。我使用 CURL,但即使我将 json_decode() 包裹在 json sring 变量周围,它仍然会返回一个字符串。有什么想法吗?

$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));

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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟酒忠诚 2024-09-06 16:04:20

您的请求 URL 结尾为:

&format=json&jsoncallback=1';

参数的正确名称是 nojsoncallback,因此您应该使用的正确 URL 结尾如下:

&format=json&nojsoncallback=1';

更改它,它应该可以工作。

问候。

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.

在巴黎塔顶看东京樱花 2024-09-06 16:04:20

这是因为返回的数据不是有效的 JSON。不过,它的 JavaScript 是有效的。
返回的数据被包装在一个名为 jsonFlickrApi 的默认回调函数中。

您需要摆脱将 JSON 包装在回调函数中的 JSON 回调,然后该回调函数应该在客户端。您需要对返回的 JSON 进行一些字符串操作以删除默认回调 jsonFlickrApi ,然后将其传递给 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 );

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 );
苏璃陌 2024-09-06 16:04:20

更好的是,不要在 url 中使用 format=json,而是使用 format=php_serial 并获取序列化字符串,这样您就不必担心 flickr 的有效格式,并且会得到一个数组作为回报

$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);

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);
没企图 2024-09-06 16:04:20

堆栈溢出再次挽救了局面。我搜索了 flickr 文档,发现没有提及这个“nojsoncallback”参数。

谁默认提供了这样的功能,然后没有告诉任何人如何禁用它?

更糟糕的是,为什么会写到您必须启用它才能禁用该功能?!

荒谬...但是感谢您的提醒,这解决了我的问题!

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!

贱人配狗天长地久 2024-09-06 16:04:20

nojsoncallback 的详细信息位于此页面底部 https: //www.flickr.com/services/api/response.json.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文