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

发布于 2024-11-01 11:04:29 字数 1242 浏览 4 评论 0原文

嗯,我正在使用 Bungie 的 Halo Reach API。现在我的代码将获取特定玩家的所有游戏 ID。

我想将游戏 ID 存储在 mysql 数据库中,然后将来如果玩家想要更新数据库,脚本将仅获取数据库中尚未存在的游戏 ID。

脚本获取最新页面$iPage = '0'; 然后,如果 HasMorePages 等于 true,它将获取下一页 $iPage++,直到 HasMorePages 为 false。 每页提供 25 个游戏 ID,最后一页可能更少。

所以基本上我想获取脚本首次运行时不存在的游戏 ID,而不对 API 进行不必要的调用。我怎样才能做到这一点?

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

?>

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

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虫児飞 2024-11-08 11:04:29

考虑到我理解你想要做什么以及你要问什么。试试这个代码:

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

我不熟悉 Bungie 可能将游戏推送到 api 的方法。如果已订购,请发表评论。我会修改我的代码。如果他们是任意的,那就运气不好了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文