1

使用以下代码,我可以获得 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)
4

1 回答 1

1

你必须设置CURLOPT_RETURNTRANSFER

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

于 2013-09-28T17:21:40.333 回答