I’m attempting to insert all results from a JSON API result, they have put a limit on max 200 results per page, although they provide the next url to be accessed to get the next 200 results, here’s an example of the result that the API provides.
{
"count": 200,
"total": 31108,
"nextUrl": "https://**/api/v1/***/orders?secure_auth_key=****&offset=200",
"orders": [
{
"number": 40026,
"paymentStatus": "ACCEPTED",
etc....
}
],
}
Here is my current code to grab these 200 results and store them in mySQL
$url = "https://****/api/v1/***/orders?secure_auth_key=*****";
$json = file_get_contents($url, 0, null, null);
$result = json_decode($json, true)
foreach($result['orders'] as $order) {
mysql_query("INSERT INTO blah blah");
}
How can i create it so it automatically gets the data from the next page e.g : offset=200 and add all the data to database like i did in the first results.
With the assumption that if there is no more results they don’t provide a URL for the next 200, why not just use that as a check.
eg. after your save simple if to check if the last result had a nexturl?
Ideally move that into a function you can continually call where needed…