Jump to content

Json Api


AndrewFerrara

Recommended Posts

My script is not acting like it should. I believe its because of the recent change in the api, (the new error array)?

 

Before there was only one....

 

<?php

$gamertag = 'l RaH l';

$url = "http://api.xboxleaders.com/user_api.php?gamertag=".str_replace(' ', '%20', $gamertag);

$output = file_get_contents($url);

// try to convert the response into a PHP object
$obj = json_decode($output);

if(!is_object($obj)) 
die("Error parsing JSON response\n");

//define variables
$avatar = ($obj->user->avatar);
$gold = ($obj->user->gold);

print $avatar;
print $gold;

?>

 

if you print_r $object;

 

it displays this

 

{"error":{"code":200,"desc":"OK"}},{"user":{"gold":1,"gamertag":"l RaH l","avatar":"http:\/\/avatar.xboxlive.com\/avatar\/l%20RaH%20l\/avatarpic-l.png","gamerscore":12508,"reputation":20},"recent_games":[{"recent_game":{"title":"Halo: Reach","link":"http:\/\/gamercard.xbox.com\/en-US\/GameCenter\/Achievements?titleId=1297287259&compareTo=l%20RaH%20l","image":"http:\/\/tiles.xbox.com\/tiles\/ih\/ew\/0Wdsb2JhbA9ECgR8GgMfVlohL2ljb24vMC84MDAwIAAAAAAAAP6fF5U=.jpg"}},{"recent_game":{"title":"Halo 3","link":"http:\/\/gamercard.xbox.com\/en-US\/GameCenter\/Achievements?titleId=1297287142&compareTo=l%20RaH%20l","image":"http:\/\/tiles.xbox.com\/tiles\/Us\/3e\/1Wdsb2JhbA9ECgR8GgMfWSpVL2ljb24vMC84MDAwIAAAAAAAAPrxzU0=.jpg"}},{"recent_game":{"title":"AC Brotherhood","link":"http:\/\/gamercard.xbox.com\/en-US\/GameCenter\/Achievements?titleId=1431504989&compareTo=l%20RaH%20l","image":"http:\/\/tiles.xbox.com\/tiles\/oK\/tp\/1Wdsb2JhbA9ECgUNGgMfVlonL2ljb24vMC84MDAwIAAAAAAAAPpGq78=.jpg"}},{"recent_game":{"title":"Fighters Uncaged","link":"http:\/\/gamercard.xbox.com\/en-US\/GameCenter\/Achievements?titleId=1431504974&compareTo=l%20RaH%20l","image":"http:\/\/tiles.xbox.com\/tiles\/v+\/Au\/0Wdsb2JhbA9ECgUNGgMfVlsmL2ljb24vMC84MDAwIAAAAAAAAP4B4KA=.jpg"}},{"recent_game":{"title":"Assassin's Creed","link":"http:\/\/gamercard.xbox.com\/en-US\/GameCenter\/Achievements?titleId=1431504852&compareTo=l%20RaH%20l","image":"http:\/\/tiles.xbox.com\/tiles\/L3\/mu\/0Wdsb2JhbA9ECgUNGgMfWStXL2ljb24vMC84MDAwIAAAAAAAAP6BeTA=.jpg"}}]}

Link to comment
Share on other sites

Well seems to me like there api is messed up. Looking at the contents of the file I was able to make a quick fix. You may want to contact them to get the issue fixed but it seems like the issue is with the error code portion. After the error code "OK" there are 2 "}" and there should only be one. The other problem is before the "user" there is a "{" and there should not be on there. The search and replace I setup allows you to pull this feed so you may want to apply that for a quick fix. I added at the bottom the part that is commented out so that you can see what it should look like if you uncomment it. You can see what I am talking about if you compare that to the first one. Also instead of doing a str_replace() on the gamertag you can just urlencode() it and it should be safe to pass through the url.

 

<?php

$gamertag = 'l RaH l';

$url = "http://api.xboxleaders.com/user_api.php?gamertag=".urlencode($gamertag);

$output = file_get_contents($url);

$search = array('"OK"}}','{"user"');
$replace = array('"OK"}','"user"');

$output = str_replace($search,$replace,$output);

// try to convert the response into a PHP object
$obj = json_decode($output);

//print_r($obj);
if(!is_object($obj)) 
die("Error parsing JSON response\n");

//define variables
$avatar = ($obj->user->avatar);
$gold = ($obj->user->gold);

print $avatar;
print $gold;

//$array['error'] = array('code'=>200,'desc'=>'ok');
//$array['user']	= array('gold'=>0,'gamertag'=>0);
//$array['recent_games']  = array(array('recent_game'=>array('title'=>'one'),'recent_game'=>array('title'=>'two')));
//echo json_encode($array);
?>

 

Link to comment
Share on other sites

I would just wrap the whole thing in an array.

$obj = json_decode("[" . $output . "]");

$obj[0] is an object with an "error" property, and $obj[1] is an object with "user" and "recent_games" properties.

 

Though I agree that the API should be fixed. It should all be one object, not an array of them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.