How to loop a function to paginate?

I’m building a plugin that will import some data from a third-party API. It will import about 1000 records. The API can only return 250 records per call. Each call gives a nextLink object that sets the next url to call.

For example, if the first call is$count=true&$top=250&$expand=Media&$skip=250" then the response includes “@odata.nextLink”: “$count=true&$top=250&$expand=Media&$skip=250” to indicate the next url to call to get the next bath of records.

$top indicates how many records to return at once, and $skip indicates how many records to skip over.

If no @odata.nextLink is returned, that means you’ve paginated through all the records.

What would the function look like to keep making Curl calls until they’ve all been imported?

there are various methods that could be employed, but in general it would flow something like…

set nextLink to original load.
while nextLink is not null
load link
set nextLink to returned value or null

Well, I like to think off skip as the offset

Offset = Number of Records Per Page * (Current Page - 1)

Total Pages = ceil(Total Count / Per Page)

JavaScript or a server-side programming language like PHP can be used to loop a function in order to generate the necessary pages for a certain site’s navigation structure. To get the information you need from the server, you’ll also need to make an API call and utilise a for loop statement.