从Riot API获取召唤者ID(php)

时间:2015-11-27 09:21:10

标签: php mysql api rate-limiting riot

我对php有点新,我遇到了一些麻烦:

我想把Challenger,Master,Diamond和Platinum中的所有汇总器都放到mysql数据库中。 为此,我一直在使用这个库https://github.com/carlos170586/php-lol-api

问题是我的托管服务不允许执行脚本超过2分钟。并且不允许更改php.ini

中的max_execution_time

另外,我在理解如何正确使用速率限制方面遇到了一些麻烦。到目前为止,由于测试API密钥,我每秒只能有1个请求,我在执行请求后只是使用了sleep(1),但我想让它在不同的速率限制下更具动态性。

我的另一个疑问是如何使这成为一个自动化过程。它应该用cron制作吗?

<?php

// Including api key, library and mysqli connect
include 'config.php';   


// Request to get challenger players (Player id and Player name) and sleep
$challenger = $api->getChallengerLeague()->entries;
sleep(1);

// For each player, get id and name and insert into database. If id is duplicated, update.
$c = 0;
foreach ($challenger as $key => $val) {

    // Player id
    $player = $challenger[$c]->playerOrTeamId;
    // Player name
    $name = $challenger[$c]->playerOrTeamName;
    $division = 'I';
    $league = 'CHALLENGER';

    $c++;

    // Insert into database
    if ($restosql = $dbgeneral->query("
    INSERT INTO summoners (player, name, division, league, patch)
    VALUES ('$player', '$name', '$division', '$league', '$patch')
    ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), name=VALUES(name), division=VALUES(division), league=VALUES(league), patch=VALUES(patch);
    ")) {
        printf("<br>id $player Name: $name league: $league");
    }
    else {
        printf("<br>Error uploading player: %s\n", $dbgeneral->error);
    }
}

// Same than the challengers for masters.
$master = $api->getMasterLeague()->entries;
sleep(1);

$m = 0;
foreach ($master as $key => $val) {

    $player = $master[$m]->playerOrTeamId;
    $name = $master[$m]->playerOrTeamName;
    $division = 'I';
    $league = 'MASTER';
    $m++;

    if ($restosql = $dbgeneral->query("
    INSERT INTO summoners (player, name, division, league, patch)
    VALUES ('$player', '$name', '$division', '$league', '$patch')
    ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), name=VALUES(name), division=VALUES(division), league=VALUES(league), patch=VALUES(patch);
    ")) {
        printf("<br>id $player Name: $name league: $league");
    }
    else {
        printf("<br>Error uploading player: %s\n", $dbgeneral->error);
    }
}


// Here is where it gets spicy.
// for DIAMONDS and PLATINUMS, select all the previously stored summoners in the database. If the summoner has not been selected previously, his status is 0. 
if ($result = $dbgeneral->query("SELECT player FROM `summoners` WHERE status = '0'")) {
    printf("Selected all players %d .\n", $result->num_rows);
}
else {
    printf("Error selecting players with status = 0: %s\n", $dbgeneral->error);
}

// A huge loop to get more summoners. It starts with the challengers and masters stored in the database. (1200 ~ summoners)
while($row = $result->fetch_assoc()) {

    // ID of the selected player
    $playerino = $row["player"];

    // Requesting the API to get last 10 games of the selected player and sleep
    $recentgames = $api->getRecentGames($playerino)->games;
    sleep(1);

    // For each of the last 10 games retrieved, look for RANKED games, get ID and name of every player in those games and the FULL league from division V to I if he is DIAMOND or PLATINUM
    $g = 0;
    foreach ($recentgames as $key => $val) {

        // Cheking if its a Ranked match
        $checkranked = $recentgames[$g]->subType;
        if($checkranked == 'RANKED_SOLO_5x5'){

            // Players of the match
            $fellows = $recentgames[$g]->fellowPlayers;

            //For each player of the ranked game, get id and league
            $f = 0;
            foreach ($fellows as $key => $val) {
                // ID of the player
                $newplayer = $fellows[$f]->summonerId;

                // Requesting API to get ranking information of the player and sleep
                $leaguefull = $api->getLeagues($newplayer);
                sleep(1);
                $leaguefull2 = $leaguefull->$newplayer;
                $newplayerleague = $leaguefull2[0]->tier;   

                // IF the player is DIAMOND or PLATINUM, get all the players of his league. From division V to I
                if($newplayerleague == 'DIAMOND' || $newplayerleague == 'PLATINUM'){

                    // All the players of the league
                    $leaguefullnames = $leaguefull2[0]->entries;    

                    $n=0;
                    foreach ($leaguefullnames as $key => $val) {
                        $player = $leaguefullnames[$n]->playerOrTeamId; // Player ID
                        $name = $leaguefullnames[$n]->playerOrTeamName; // Player 
                        $division = $leaguefullnames[$n]->division; // I, II, III, IV, V
                        $league = $newplayerleague; // DIAMOND or PLATINUM

                        // Insert each player into database. If the playes was already there, update his name, division and league (Maybe he has changed name or climbed)
                        if ($restsql = $dbgeneral->query("
                        INSERT INTO summoners (player, name, division, league, patch)
                        VALUES ('$player', '$name', '$division', '$league', '$patch')
                        ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), name=VALUES(name), division=VALUES(division), league=VALUES(league), patch=VALUES(patch);")) {
                            printf("<br>Game $g player $f / $n id: $player name: $name league: $league division: $division");
                        }
                        else {
                            printf("<br>Error uploading player: %s\n", $dbgeneral->error);
                        }
                        $n++;
                    }
                }
                $f++;
            }
        }
        $g++;
    }

    // Set status of the selected summoner to 1 and continue with the next summoner.
    if ($changestatus = $dbgeneral->query("UPDATE summoners SET status='1' WHERE status='0' and player='$playerino'")) {
        printf("<br><br><br>Status of $playerino changed");
    }
    else {
        printf("<br>Error changing status: %s\n", $dbgeneral->error);
    }
}


$dbgeneral->close();
?>

我觉得这很糟糕,而且必须是更好的方法。

0 个答案:

没有答案