kevin seda
kevin seda

Reputation: 406

API call in loop

Im making a big APi call, i have over 6000 records. Out of those 6000 i get 200 per page. So if we divide 6000 by 200 we get 30 pages in total.

So if I would want to get all records I'd need to make 30 different requests. I can speficy the amount per page in my request url parameters. Right now it looks like this:

$getRequest = $CallMonkeyApi->makeApiCall('GET','/address/get-all?apiKey=secret&page=1&size=200');

In the url you see a parameter "page=1" I would like this number to be dynamic from a loop that stops at 30. But I dont know where to start.

Upvotes: 0

Views: 468

Answers (1)

Harvey Fletcher
Harvey Fletcher

Reputation: 1172

You can do this with a "for" loop

//An array for results
$getRequests = array();

//For every occurance where `x` is less than or equal to 30
for( $x = 1; $x<=30; $x++ ){
    //Build the path to API call
    $path = "/address/get-all?apiKey=secret&page=" . $x . "&size=200";

    //Make API call and store the output on the $getRequests array of results
    $getRequests[] = $CallMonkeyApi->makeApiCall('GET', $path);
}

Then you can access your $getRequests array by using $getRequests[$pageNumber]. So, for example, if I wanted to see page 5, i'd do print_r( $getRequests[4] ).

Note that the page number will be one less in the array because arrays start at 0.

Upvotes: 2

Related Questions