Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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) { }
like image 523
ThemesCreator Avatar asked Aug 10 '15 13:08

ThemesCreator


People also ask

How does pagination work in API?

Pagination is a process that is used to divide a large dataset into smaller chunks (pages). All Square API endpoints that return a list of resources support pagination. For example, the SearchCustomers endpoint (Customers API) and ListCustomerRefunds endpoint (Refunds API) support pagination.

What is the best approach for pagination in API?

Offset Pagination This is the simplest form of paging. Limit/Offset became popular with apps using SQL databases which already have LIMIT and OFFSET as part of the SQL SELECT Syntax. Very little business logic is required to implement Limit/Offset paging.


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

like image 114
m1lt0n Avatar answered Nov 04 '22 05:11

m1lt0n


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.

like image 43
mdamia Avatar answered Nov 04 '22 04:11

mdamia