使用 PHP、curl 解码从 Flickr API 返回的 json 字符串
我试图在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的请求 URL 结尾为:
参数的正确名称是 nojsoncallback,因此您应该使用的正确 URL 结尾如下:
更改它,它应该可以工作。
问候。
Your request URL ends with:
The correct name of the parameter is nojsoncallback, so the right URL you should be using ends like this:
Change that and it should work.
Regards.
这是因为返回的数据不是有效的 JSON。不过,它的 JavaScript 是有效的。
返回的数据被包装在一个名为
jsonFlickrApi
的默认回调函数中。您需要摆脱将 JSON 包装在回调函数中的 JSON 回调,然后该回调函数应该在客户端。您需要对返回的 JSON 进行一些字符串操作以删除默认回调
jsonFlickrApi
,然后将其传递给json_decode
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 tojson_decode
更好的是,不要在 url 中使用 format=json,而是使用 format=php_serial 并获取序列化字符串,这样您就不必担心 flickr 的有效格式,并且会得到一个数组作为回报
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
堆栈溢出再次挽救了局面。我搜索了 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!
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