paginated api request, how to know if there is another page?

From your input, in the while loop, you change the $url (which actually holds the data return by the API call) and this is checked for emptiness, if I'm correct.

$url = $my_class->get_data($sales_url);

If the above is just the original response (so in case of page out of range a string "[]"), it will never get empty("[]") to true. So my guess is that the return value from get_data is this string, while it should be the actual array/json even if the result is empty (ie I suspect that you perform the json_decode once you have collected the data e.g. outside the loop).

If this is the case, my suggestion would be to either check for "[]" in the loop (e.g. while ($url !== "[]")) or within the loop decode the response data ($url = json_decode($url)).


From my experience with several API's, the response returns the number of rows found, and x number per page starting with page 1. In your case, if the response has the number of rows then just divide it by the x number page and loop through the results as page numbers.

$results = 1000;
$perPage = 50;
$pages = ceil($results/$perPage);
for (i=1; $i <= $pages; $i++){
     // execute your api call and store the results
}

Hope this help.