So I have been working on an API app using Riot's API. And I need to store massive amounts of API endpoint data into a SQL DB so I can pull stats from the DB.
I have tested just about everything I can, no spelling errors, no connection errors, but for some reason the data is never getting loaded into the server and the page is just stuck on refreshing.
This initially made me think it was an infinite loop, however I tested the loop and it is not an infinite loop, or at least as far as I could test.
My next thought was that this script will just take awhile to run, so I waited for over an hour and it still was stuck on loading the page.
I haven't even gotten to update statements yet, but if you guys could take a look at my code and see if you're finding anything wrong or even if there is a better approach to do this, thanks!
Also if you need any more information or questions about my goals/layout feel free to ask for clarification.
Also I've removed the apiKey and db credentials for obvious reasons, if you need an API key to test the script they are avaialable at https://developer.riotgames.com/
<?php
$data = [
'IRON' => 'I',
'IRON' => 'II',
'IRON' => 'III',
'IRON' => 'IV',
'BRONZE' => 'I',
'BRONZE' => 'II',
'BRONZE' => 'III',
'BRONZE' => 'IV',
'SILVER' => 'I',
'SILVER' => 'II',
'SILVER' => 'III',
'SILVER' => 'IV',
'GOLD' => 'I',
'GOLD' => 'II',
'GOLD' => 'III',
'GOLD' => 'IV',
'PLATINUM' => 'I',
'PLATINUM' => 'II',
'PLATINUM' => 'III',
'PLATINUM' => 'IV',
'DIAMOND' => 'I',
'DIAMOND' => 'II',
'DIAMOND' => 'III',
'DIAMOND' => 'IV'
];
function update_DB($param, $region)
{
foreach ($param as $tier => $division) {
$page = 1;
while ($page >= 1) {
$apiKey = '';
$url = 'https://' . $region . '.api.riotgames.com/tft/league/v1/entries/' . $tier . '/' . $division . '?page=' . $page . '&api_key=' . $apiKey;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
$output = curl_exec($curl);
curl_close($curl);
$arr = json_decode($output, true);
if ($arr === []) { //Empty Quit Loop
$page = 0;
}
if (count($arr) > 0) { //Not empty move to next page and store/update data
$i = 0;
while ($i < count($arr)) {
$conn = mysqli_connect("localhost", "root", "", "");
$leagueId = $arr[$i]['leagueId'];
$queueType = $arr[$i]['queueType'];
$tier = $arr[$i]['tier'];
$rank = $arr[$i]['rank'];
$summonerId = $arr[$i]['summonerId'];
$summonerName = $arr[$i]['summonerName'];
$leaguePoints = $arr[$i]['leaguePoints'];
$wins = $arr[$i]['wins'];
$losses = $arr[$i]['losses'];
$stmt = $conn->prepare("INSERT INTO allusersna (leagueId, queueType, tier, rank, summonerId, summonerName, leaguePoints, wins, losses) VALUES (?,?,?,?,?,?,?,?,?)");
if (!$stmt) {
echo 'ERROR INSERT STATEMENT';
exit();
}
$stmt->bind_param("ssssssiii", $leagueId, $queueType, $tier, $rank, $summonerId, $summonerName, $leaguePoints, $wins, $losses);
$stmt->execute();
$i++;
}
$page++;
}
}
}
}