Home » Php » php – paginated api request, how to know if there is another page?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am creating a PHP class that use a 3rd party API. The API has a method with this request URL structure:

https://api.domain.com/path/sales?page=x

Where “x” is the page number.

Each page return 50 sales and I need to return an undefined number of pages for each user (depending on the user sales) and store some data from each sale.

I have already created some methods that get the data from the URL, decode and create a new array with the desired data, but only with the first page request.

Now I want to create a method that check if is there another page, and if there is, get it and make the check again

How can I check if there is another page? And how to create a loop that get another page if there is one?

I have already this code, but it create an infinite loop.

require('classes/class.example_api.php');
$my_class = new Example_API;
$page = 1;
$sales_url = $my_class->sales_url( $page );
$url = $my_class->get_data($sales_url);

while ( !empty($url) ) {
    $page++;
    $sales_url = $my_class->sales_url( $page );
    $url = $my_class->get_data($sales_url);
}

I don’t use CURL, I use file_get_content. When I request a page out of range, I get this result:

string(2) "[]"

And this other after json_decode:

array(0) { }
How to&Answers:

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)).

Answer:

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.

Answer:

From the responses you’ve shown, you get an empty array if there are no results. In that case, you could use the empty method in a loop to determine if there’s anything to report:

// Craft the initial request URL
$page = 1;
$url = 'https://api.domain.com/path/sales?page=' . $page;

// Now start looping
while (!empty(file_get_contents($url)) {
    // There's data here, do something with it

    // And set the new URL for the next page
    $url = 'https://api.domain.com/path/sales?page=' . ++$page;
}

That way it will keep looping over all the pages, until there is no more data.

Answer:

Check http response headers for total number of items in set