1

I have a PHP script supposed to insert data to a database on a web server. I got around 360 records. If I run the script only inserting one or two columns everything is OK. If i run the script feeding 10 columns or more, the script stop at 20-50 records. Why is this, and what can I do to make the script insert all records and all columns?

This is the first script, working perfectly:

<?php


$tilkobling = new mysqli($servername, $username, $password, $dbname);

if ($tilkobing->connect_error) { //Mislykket DV-tilkobling
    die("Kunne ikke koble til database.");
  }



$kommune_api_url = "https://register.geonorge.no/api/sosi-kodelister/kommunenummer.json?";
$kommune_api_response = file_get_contents($kommune_api_url);
$kommune_api_data = json_decode($kommune_api_response,true);

$kommune_antall = $kommune_api_data['ContainedItemsResult']['Count'];

// LEGGER TIL KOMMUNENUMMER I DB.
for ($kom = 0; $kom <= $kommune_antall; $kom++) {
    $kommunenummer = $kommune_api_data['containeditems'][$kom]['codevalue'];
    $kommune_navn = $kommune_api_data['containeditems'][$kom]['description'];

    // Legger til kommunenummer i kommunenummer
    $sql = "INSERT INTO kommuneinfo (kommunenummer, kommunenavn)
    VALUES ($kommunenummer, '$kommune_navn')";

    if (mysqli_query($tilkobling, $sql)) {
    echo "";
    } else {
    echo "Error! Kommunenummer" . $kommunenummer . " / " . $kommune_navn . " ble ikke oppdatert.<br>" . mysqli_error($conn);
    }
    }

?>

When reading from another API and inserting much more data this is the script, which fails after 20-50 insertions:

<?php


$servername = "xxxxxx";
$username = "xxxxx";
$password = "xxxx";
$dbname = "xxxx";

$tilkobling = new mysqli($servername, $username, $password, $dbname);

if ($tilkobing->connect_error) { //Mislykket DV-tilkobling
    die("Kunne ikke koble til database.");
  }



$kommune_api_url = "https://register.geonorge.no/api/sosi-kodelister/kommunenummer.json?";
$kommune_api_response = file_get_contents($kommune_api_url);
$kommune_api_data = json_decode($kommune_api_response,true);

$kommune_antall = $kommune_api_data['ContainedItemsResult']['Count'];

// LEGGER TIL KOMMUNENUMMER I DB.
for ($kom = 0; $kom <= 360; $kom++) {
    $kommune = $kommune_api_data['containeditems'][$kom]['codevalue'];
    $kommune_navn = $kommune_api_data['containeditems'][$kom]['description'];
    
    if ($kommune != "2211") {
        echo $kommune_navn . "<br>";
        echo $kom . "<br>";
    
        $url = "https://data.ssb.no/api/v0/no/table/06913/";
    
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
        $headers = array(
        "Accept: application/json",
        "Content-Type: application/json",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    
        $data = <<<DATA
        {
        "query": [
            {
            "code": "Region",
            "selection": {
                "filter": "agg:KommSummerHist",
                "values": [
                "K_$kommune"
                ]
            }
            },
            {
            "code": "ContentsCode",
            "selection": {
                "filter": "item",
                "values": [
                "Folkemengde"
                ]
            }
            },
            {
            "code": "Tid",
            "selection": {
                "filter": "item",
                "values": [
                "1970",
                "1971",
                "1972",
                "1973",
                "1974",
                "1975",
                "1976",
                "1977",
                "1978",
                "1979",
                "1980",
                "1981",
                "1982",
                "1983",
                "1984",
                "1985",
                "1986",
                "1987",
                "1988",
                "1989",
                "1990",
                "1991",
                "1992",
                "1993",
                "1994",        
                "1995",
                "1996",
                "1997",
                "1998",
                "1999",
                "2000",
                "2001",
                "2002",
                "2003",
                "2004",
                "2005",
                "2006",
                "2007",
                "2008",
                "2009",
                "2010",
                "2011",
                "2012",
                "2013",
                "2014",
                "2015",
                "2016",
                "2017",
                "2018",
                "2019",
                "2020",
                "2021"
                ]
            }
            }
        ],
        "response": {
            "format": "json-stat2"
        }
        }
        DATA;
    
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    
        //for debug only!
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
        $resp = curl_exec($curl);
        curl_close($curl);
        //var_dump($resp);
    
        $test = json_decode($resp, true);
        $befolkning1970 = $test['value'][0];
        $befolkning1971 = $test['value'][1];
        $befolkning1972 = $test['value'][2];
        $befolkning1973 = $test['value'][3];
        $befolkning1974 = $test['value'][4];
        $befolkning1975 = $test['value'][5];
        $befolkning1976 = $test['value'][6];
        $befolkning1977 = $test['value'][7];
        $befolkning1978 = $test['value'][8];
        $befolkning1979 = $test['value'][9];
        $befolkning1980 = $test['value'][10];
        $befolkning1981 = $test['value'][11];
        $befolkning1982 = $test['value'][12];
        $befolkning1983 = $test['value'][13];
        $befolkning1984 = $test['value'][14];
        $befolkning1985 = $test['value'][15];
        $befolkning1986 = $test['value'][16];
        $befolkning1987 = $test['value'][17];
        $befolkning1988 = $test['value'][18];
        $befolkning1989 = $test['value'][19];
        $befolkning1990 = $test['value'][20];
        $befolkning1991 = $test['value'][21];
        $befolkning1992 = $test['value'][22];
        $befolkning1993 = $test['value'][23];
        $befolkning1994 = $test['value'][24];
        $befolkning1995 = $test['value'][25];
        $befolkning1996 = $test['value'][26];
        $befolkning1997 = $test['value'][27];
        $befolkning1998 = $test['value'][28];
        $befolkning1999 = $test['value'][29];
        $befolkning2000 = $test['value'][30];
        $befolkning2001 = $test['value'][31];
        $befolkning2002 = $test['value'][32];
        $befolkning2003 = $test['value'][33];
        $befolkning2004 = $test['value'][34];
        $befolkning2005 = $test['value'][35];
        $befolkning2006 = $test['value'][36];
        $befolkning2007 = $test['value'][37];
        $befolkning2008 = $test['value'][38];
        $befolkning2009 = $test['value'][39];
        $befolkning2010 = $test['value'][40];
        $befolkning2011 = $test['value'][41];
        $befolkning2012 = $test['value'][42];
        $befolkning2013 = $test['value'][43];
        $befolkning2014 = $test['value'][44];
        $befolkning2015 = $test['value'][45];
        $befolkning2016 = $test['value'][46];
        $befolkning2017 = $test['value'][47];
        $befolkning2018 = $test['value'][48];
        $befolkning2019 = $test['value'][49];
        $befolkning2020 = $test['value'][50];
    
    
    
    
    
    
        // Legger til kommunenummer i kommunenummer
        $sql = "UPDATE kommuneinfo
        set befolkning1970 = $befolkning1970,
        befolkning1975 = $befolkning1975,
        befolkning1980 = $befolkning1980,
        befolkning1985 = $befolkning1985,
        befolkning1990 = $befolkning1990,
        befolkning1995 = $befolkning1995,
        befolkning2000 = $befolkning2000,
        befolkning2005 = $befolkning2005,
        befolkning2010 = $befolkning2010,
        befolkning2015 = $befolkning2015,
        befolkning2020 = $befolkning2020
        WHERE kommunenummer = $kommune";
    
    
    
    
    
    
        if (mysqli_query($tilkobling, $sql)) {
        echo "";
        } else {
        echo "Error! Kommunenummer" . $kommune . " / " . $kommune_navn . " ble ikke oppdatert.<br>" . mysqli_error($conn);
        }
        } 
    }

echo "<br>Ferdig";



?>
0

1 Answer 1

1

I ran your code.

First I got rid of all the database-related stuff, because I don't think the problem is the database. Then I added curl_error(), like this:

$resp = curl_exec($curl);
echo curl_error($curl);
curl_close($curl);

because I suspect that curl is the real problem. And indeed I get a nice error back after 61 calls:

{"error":"429 - Too many requests in too short timeframe. Please try again later."}.

That's because you try to make 360 requests in a loop. The API doesn't like that.

I've a bit further away from Norway, so my calls are done a bit slower and it takes a bit longer to reach the limit than it did with you. This hints at an easy and lazy way to solve this problem. You could add a long enough usleep() inside the loop to slow it down just enough so you don't hit the limit.

A better way to solve this problem would be if it was possible to make less calls to get the same data. I don't know if this is possible, but my guess is that it is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.