Inserting all the Gamertags from a game history page.?
I can't figure out how to get all the gamertags from the output, and then insert them into a mysql table.
This is what I have so far, read the comments... Thank you!
<?php
//gameID
$gameid = "469547013";
//key to access api
$apikey = "30cRxVA9J73esG388CzmOXUVRo5VjYhSfI2qBaqcMzs=";
$url = "http://www.bungie.net/api/reach/reachapijson.svc/game/details/".$apikey."/".$gameid."";
$output = file_get_contents($url);
$obj = json_decode($output);
//output
print_r($output);
//json_decoded output
print_r($obj);
//Having a hard time getting a single gamertag
print $obj->GameDetails->Players->PlayerDetail[0]->gamer开发者_如何学Pythontag;
//I guess there needs to be an array or arrays here
$result = ????????????;
while ($tag = $result) {
//value
$tag = $array['gamertag'];
//insert each gamertag into table
mysql_connect('localhost', '', '') or die('Error connecting to MySQL');
mysql_select_db('');
mysql_query("INSERT IGNORE INTO gamertags(gamertag)VALUES ('".$tag."')");
}
?>
Check this out:
$output = file_get_contents('http://www.bungie.net/api/reach/reachapijson.svc/game/details/30cRxVA9J73esG388CzmOXUVRo5VjYhSfI2qBaqcMzs=/469547013');
$output = json_decode($output);
foreach($output->GameDetails->Players as $player) {
echo $player->PlayerDetail->gamertag . '<br />';
}
edit Take a look at http://dbug.ospinto.com/. I through the decoded json into it and it told me exactly how to get what I needed.
Players is an array, so you'll want something like this:
print $obj->GameDetails->Players[0]->PlayerDetail->gamertag;
So then you'll want to loop through the Players
array and get each tag. Something like this:
$ary = new Array();
foreach($obj->GameDetails->Players as $player) {
$ary[] = $player->PlayerDetail->gamertag;
}
精彩评论