Displaying pages in the program, how to implement the page paging method?

I am working on implementing an API for my project.

As I know there are various forms for pagination by results, for example:

https://example.com/api/purchaseorders?page=2&pagesize=25  

      

But I see that many APIs like google take a different approach in which they use a "pageToken" so that the user can navigate between the results pages, for example:

https://example.com/api/purchaseorders?pagesize=25&pageToken=ClkKHgoRc291cmNlX2NyZWF0ZWRfYXQSCQjA67Si5sr

      

So page=2

they used instead pageToken=[token]

.

I don't understand the idea of โ€‹โ€‹pageToken and how to implement it.

It will be helpful if you guide me to any resources so that I can gain more knowledge.

Thank.

+3


source to share


1 answer


Here is a very simple single example of using the file system as a keystore (since the file system will always be available).



$requestParameters = [];
if (($token = filter_input(INPUT_GET,"pageToken")) && is_readable("/tmp/$token")) {
   $requestParameters = file_get_contents("/tmp/$token");
} else {   
    $requestParameters = [
       "q" => filter_input(INPUT_GET,"q"),
       "pageSize" => filter_input(INPUT_GET,"pageSize",FILTER_VALIDATE_INT),
       "page" => filter_input(INPUT_GET,"page",FILTER_VALIDATE_INT)
   ];
}

$nextPageRequestParameters = $requestParameters;
$nextPageRequestParameters["page"]++;

$nextPageToken = md5(serialize($nextPageRequestParameters)); //This is not ideal but at least people can't guess it easily. 


file_put_contents("/tmp/$nextPageToken", serialize($nextPageRequestParameters));

//Do request using $requestParameters 
$result = [ "nextPageToken" => $nextPageToken, "data" => $resultData ];
echo json_encode($result);

      

0


source







All Articles