0

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

}

  • were you able to fetch data from API ? – my_workbench Aug 09 '20 at 02:29
  • and what is the use of `$data` ? also, whatever the use is, it has duplicate keys, and is not what you think it is. – YvesLeBorg Aug 09 '20 at 02:29
  • what is the purpose of the `$data` array before the function ?, did you notice that you use a same keys in this array a lot of time ? – GNassro Aug 09 '20 at 02:29
  • So the data is returning correctly and in a valid format. and for @YvesLeBorg $data is the search parameters for the cURL url. as i want to iterate through multiple searches in one call. So $data is an array with the $key=>$value pairs($tier=>$division) and this is to be used in the foreach() loop . – chrisalopez1337 Aug 09 '20 at 02:32
  • @GNassro So data is because the parameters of the $url take multiple iterations : the region, page number, apiKey, but also the tier and division search so $data -> $params as $tier=>$division. And I am aware I reuse the keys, however the data is actually being retrieved just fine. – chrisalopez1337 Aug 09 '20 at 02:35
  • i didn't say the problem from `$data`, just i asked to know the purpose of it in your code – GNassro Aug 09 '20 at 02:37
  • 1
    and yes i think you have infinite loop in your code, `$page >= 1` alway true in the first while loop – GNassro Aug 09 '20 at 02:40
  • @GNassro sorry for the misunderstanding, and so eventually the $page in the search will get to a point where the JSON data returned is and empty array [] and if you notice I did set a stopping condition if($arr = []) { $page = 0}. Would this not work in stopping the loop? – chrisalopez1337 Aug 09 '20 at 02:42
  • 1
    nope this is will not stopping the loop, because in the end of the first while loop, you have `$page++`, so `$page` will equal to 1, so `$page >= 1` will be true – GNassro Aug 09 '20 at 02:44
  • @GNassro ah okay that makes sense, so If i set it to like -10 instead of 0 it should stop. Thanks! – chrisalopez1337 Aug 09 '20 at 02:45

0 Answers0