dongyang5716 2011-04-13 19:55
浏览 65
已采纳

如何更新游戏历史数据库? Bungie Halo Reach API

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++;
}

?>
  • 写回答

1条回答 默认 最新

  • dongshui2254 2011-04-13 20:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址