Я немного новичок в php, и у меня возникли некоторые проблемы с этим:
Я хотел бы получить всех призывателей в Challenger, Master, Diamond и Platinum в базе данных MySQL.
Для этого я использую эту библиотеку https://github.com/carlos170586/php-lol-api
Проблема в том, что мой хостинг не позволяет выполнять скрипты дольше 2 минут. И не разрешено изменять max_execution_time в php.ini
Кроме того, у меня возникли проблемы с пониманием того, как правильно использовать ограничение скорости. До сих пор, поскольку из-за 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();
?>
У меня такое чувство, что это ужасно и что это должен быть лучший способ сделать это.
Задача ещё не решена.
Других решений пока нет …