无法解码从API返回的JSON

时间:2013-09-28 14:47:17

标签: php arrays json api

使用以下代码,我能够获得响应,JSON格式化,但我需要PHP数组中的数据:

$url = 'https://xboxapi.com/games/Major+Nelson';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLVERSION,3); 
$result = curl_exec($ch);
curl_close($ch);

var_dump(json_decode($result, true));

如果我接受API返回的字符串并将其设置为变量,我可以将其转换为数组,如果我找到并用'替换每个\'。我错过了什么?某个地方的内容类型?

这是输出,大部分中间被修剪掉:

起点。:

{"Success":true,"API_Limit":"26\/350","Player":{"Gamertag":"Major Nelson","Avatar":{"Gamertile":{"Small":"https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major%20Nelson\/avatarpic-s.png","Large":"https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major%20Nelson\/avatarpic-l.png"},"Gamerpic":{"Small":"https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major Nelson\/avatarpic-s.png","Large":"https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major Nelson\/avatarpic-l.png"},"Body":"https:\/\/avatar-ssl.xboxlive.com\/avatar\/Major Nelson\/avatar-body.png"},"Gamerscore":64367,"GameCount":791,"PercentComplete":13},"Games":[{"ID":1414793383,"Name":"Grand Theft Auto V","MarketplaceURL":"http:\/\/marketplace.xbox.com\/en-US\/Title\/1414793383","BoxArt":{"Small":"http:\/\/catalog.xboxapi.com\/image\/66acd000-77fe-1000-9115-d802545408a7\/boxartsm.jpg","Large":"http:\/\/catalog.xboxapi.com\/image\/66acd000-77fe-1000-9115-d802545408a7\/boxartlg.jpg"},"PossibleScore":1000,"PossibleGamerscore":1000,"PossibleAchievements":49,"Progress":{"Score":30,"Achievements":2,"LastPlayed":"\/Date(1380344435573)\/","LastPlayed-UNIX":1380344435},"CatalogLink":"http:\/\/catalog.xboxapi.com\/1414793383","AchievementInfo":"https:\/\/xboxapi.com\/achievements\/1414793383\/Major+Nelson"},

结束:

{"ID":1096157139,"Name":"Gun","MarketplaceURL":"http:\/\/marketplace.xbox.com\/en-US\/Title\/1096157139","BoxArt":{"Small":"http:\/\/catalog.xboxapi.com\/image\/66acd000-77fe-1000-9115-d802415607d3\/boxartsm.jpg","Large":"http:\/\/catalog.xboxapi.com\/image\/66acd000-77fe-1000-9115-d802415607d3\/boxartlg.jpg"},"PossibleScore":1000,"PossibleGamerscore":1000,"PossibleAchievements":31,"Progress":{"Score":5,"Achievements":1,"LastPlayed":"\/Date(1132028299137)\/","LastPlayed-UNIX":1132028299},"CatalogLink":"http:\/\/catalog.xboxapi.com\/1096157139","AchievementInfo":"https:\/\/xboxapi.com\/achievements\/1096157139\/Major+Nelson"}]}int(1)

1 个答案:

答案 0 :(得分:1)

您必须设置CURLOPT_RETURNTRANSFER

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

否则curl只打印结果并返回 1 - 这就是为什么最后有 int(1)的原因。