I'm trying to parse an API response that can have thousands of results so it provides a @odata.nextLink
. I want to return all of the results in an array so after I make a curl request that has a nextLink
, I use php aray union to add to the previous array with the result
$result_array = $api_response['value']; //store initial response
while (array_key_exists('@odata.nextLink', $api_response)) {
curl_setopt_array($curl, array(
CURLOPT_URL => $api_response['@odata.nextLink'],
(more options)...
)):
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
$api_response = json_decode($response, TRUE);
$result_array = $result_array + $api_response['value'];
}
}
A response looks like this
{
"@odata.context":"https:/api.com/v1/query","value":[
{
"Sku":"ABC","ID":123
},
(more records)...
],"@odata.nextLink":"https://api.com/v1/query&$skip=200"
}
array looks like this with print_r
(
[0] => Array
(
[Sku] => abc
[ID] => 123
)
[1] => Array
(
[Sku] => efg
[ID] => 456
)
(more results)...
)
However, it stops adding elements to the array after 5000. I execute the script file from php -f
for testing. I try ini_set('memory_limit', '1000M');
like suggested to overcome memory limit but it doesn't help. Is this some sort of limitation for the PHP CLI? On a live server would this still happen? Eventually I'd like to make another curl request on each product id in a large data set (probably in a batch) to see if the api can handle it.
Edit: When I meant set up curl, I was changing the curl request to next link. I added the change for clarification and show that it doesn't loop infinitely.
Edit 2: OK so I have a better deal of what's happening. The API returns the value
as a 2d array, each inner array is a different record. It returns 100 results a few times until it serves 5000. The array key resets each time. The after setting the initial $result_array
is set to first 100 results, the plus operator doesn't append the next 100 since they are the same keys. Once API starts returning 5000 results, it appends from index 100 to 4999. Subsequent requests don't add more like previously. Is there a an array function in php that can append multidimensional arrays to each other like + does with one dimensional arrays? I could do a foreach
to append properly but it seems tedious if there's already a built-in method.