Chords
Chords

Reputation: 6860

Unable to decode JSON returned from API

Using the following code, I'm able to get a response, JSON formatted, but I need the data in a PHP array:

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

If I take the string the API is returning and set it to a variable, I can convert it to an array if I find and replace every ' with \'. What am I missing? A content type someplace?

Here is the output, with much of the middle trimmed out:

Beginning:

{"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"},

End:

{"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)

Upvotes: 1

Views: 798

Answers (1)

Nebril
Nebril

Reputation: 888

You have to set CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Otherwise curl just prints the result and returns 1 - this is why there's int(1) at the very end.

Upvotes: 1

Related Questions