Paginated api request, how to find out if there is another page?

I am creating a PHP class that uses a third-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 returns 50 sales, and I need to return an undefined number of pages for each user (depending on the user's sales) and save some data from each sale.

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

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

How to check if there is another page? And how to create a loop that gets another page, if any?

I already have this code, but it creates 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 do not use CURL, I use file_get_content . When I request a page out of range, I get this result:

 string(2) "[]" 

And this is different after json_decode :

 array(0) { } 
+8
api php pagination
source share
4 answers

From your input in the while loop, you change the $ url (which actually returns data when the API is called), and this is checked for emptiness, if I'm right.

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

If the above is only the original answer (therefore, if the page is out of range, the string "[]"), it will never become empty ("[]") to the truth. Therefore, I assume that the return value from get_data is a string, while it should be the actual / json array, even if the result is empty (i.e. I suspect you are executing json_decode after you have collected the data, e.g. out of cycle).

If so, my suggestion would be to either check "[]" in the loop (for example, while ($ url! == "[]")), or decode the response data ($ url = json_decode ($ url)) inside the loop.

+2
source share

Based on my experience with several APIs, the answer returns the number of rows found and the number x per page, starting from page 1. In your case, if the answer has a number of rows, simply divide it into a page with number x and loop through the results in 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 helps.

+1
source share

From the answers you showed, you get an empty array if there are no results. In this case, you can use the empty method in the loop to determine if there is 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 data here, do something with it // And set the new URL for the next page $url = 'https://api.domain.com/path/sales?page=' . ++$page; } 

Thus, it will move across all pages until there is more data.

0
source share

Check HTTP response headers for the total number of elements in the set

0
source share

All Articles