Skip to content
Advertisement

Fetch values from multiple json array urls

I need to get the [result]->[data]->[id]->xx informations out of extern item-API json arrays. I want to save them in a mysql table (items).

Example url to the first json array:

https://eu.api.blizzard.com/data/wow/search/item?namespace=static-eu&orderby=id&_pageSize=1000&id=[1,]&_page=1&locale=de_DE&access_token=USPqoKWmdURvO1mmDeW5vRghI5dojO13XZ

You see the [1,] in the url. This is the minimum item ID from wehere I want to start fetching.

The maximum items one json array url can show is 1000. The API has over 170.000 items for which I need their particular [data] [id]. The API calls are limited to 100 per second and 36.000 per hour.

My idea is to get the last [data] [id] value from the current array. Then in the next loop I add 1 to the last arrays´ [data] [id] value as the starting point for the next loop. And so on.

In the example url the last item id is 2429. So in the next loop 2430 would be the starting point.

https://eu.api.blizzard.com/data/wow/search/item?namespace=static-eu&orderby=id&_pageSize=1000&id=[2430,]&_page=1&locale=de_DE&access_token=USPqoKWmdURvO1mmDeW5vRghI5dojO13XZ

This is my code. I don´t know how to loop this and if this would be possible to do. Maybe there is an easier solutiuon.

 //**Decode JSON in PHP ARRAY**//

function getSslPage($url, $userAgent)
{
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}

$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';

//**GET the [DATA] [ID] value of the last KEY in the CURRENT array**//

$all_values = array_values($data['results']);
$last_value = end($all_values);

//**In the next LOOP the [DATA] [ID] VALUE should be +1 as the new starting point**//

$startingItem = $last_value['data']['id'] + 1;

$url = "https://eu.api.blizzard.com/data/wow/search/item?namespace=static-eu&orderby=id&_pageSize=1000&id=[$startingItem,]&_page=1&locale=de_DE&access_token=USPqoKWmdURvO1mmDeW5vRghI5dojO13XZ";

$data = getSslPage($url, $userAgent);

//** INSERT in database **//

foreach ($data['results'] as $entry) {

       $sqle= "REPLACE INTO `items`
                (`id`)
            VALUES
                ('{$entry['data']['id']}')";
}

Advertisement

Answer

If you’re using PHP version > 7.3 you can retrieve the last index key using array_key_last()

By the way you are retrieving your last key using

end($all_values).

Watch out for the comma (,) at the end of that index, you should trim your index string to contain only numbers, try with

$newkey = trim($key, ',')

Then you can retrieve its int value with intval()

$intnewkey = intval($newkey).

Now you will be able to increment your index with

$intnewkey++;,

return back to string with strval()

$newIndex = strval($intnewkey)

and then append again the comma to your new value for you API Call:

$newIndexForAPI = $newIndex . ','

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement