Well, I'm working with Bungie's Halo Reach API. Right now my code will get all the game ids for a particular player.
I want to store the game ids in a mysql database and then in the future if a player wants to update the database the script will only get the game ids that aren't already in the database.
The script gets the most recent page $iPage = '0';
Then if HasMorePages
is equal to true it gets the next page $iPage++
until HasMorePages
is false. Each page gives 25 game ids the last page may have less.
So basically I want to get game ids that weren't there when the script was first run, without making unnecessary calls to the API. How could I do that?
<?php
include_once('sql.php'); // MySQL Connection
include_once('api.php'); // API unique identifer string
$gamertag = 'jam1efoster'; // Gamertag
$variant = 'Unknown'; // Unknown gets all game variants
$iPage = '0'; // 0 is the most recent page
while(!$endPages == true){
$GetGameHistory = "http://www.bungie.net/api/reach/reachapijson.svc/player/gamehistory/".$apiKey."/".rawurlencode(strtolower($gamertag))."/".$variant."/".$iPage;
$output = file_get_contents($GetGameHistory);
$obj = json_decode($output);
//echo $output;
$mPages = $obj->HasMorePages;
if($mPages == false){$endPages = true;}
foreach($obj->RecentGames as $recentgames) {
$gameId = $recentgames->GameId;
//echo $gameId.'<br />';
}
//echo $iPage.'<br />';
$iPage++;
}
?>
Considering I understand what you're trying to do and what you're asking. Try this code:
<?php
include_once('sql.php'); // MySQL Connection
include_once('api.php'); // API unique identifer string
$gamertag = 'jam1efoster'; // Gamertag
$variant = 'Unknown'; // Unknown gets all game variants
$iPage = '0'; // 0 is the most recent page
// get current ids
$result=mysql_query('SELECT ALL CURRENT IDS');// PUT YOUR SQL HERE !
$oldIds=array();
$newIds=array();
while($row=mysql_fetch_array($result))$oldIds[]=$row['id'];// might be different in your scenario
// get all ids, unfortunately
for(;;){
$GetGameHistory = "http://www.bungie.net/api/reach/reachapijson.svc/player/gamehistory/".$apiKey."/".rawurlencode(strtolower($gamertag))."/".$variant."/".$iPage;
$output = file_get_contents($GetGameHistory);
$obj = json_decode($output);
// get fresh ids
foreach($obj->RecentGames as $recentgames) {
if(in_array($recentgames->GameId, $oldIds))continue;// we already have this id
$newIds[]=$recentgames->GameId;
}
if(!$obj->HasMorePages)break;// no more pages? break!
$iPage++;
}
var_dump($newIds);
?>
I'm unfamiliar with the method bungie might be pushing the games to the api. If they are ordered, comment. And I'll revise my code. If they are arbitrary, kind of tough luck.