Search code examples
phpfor-loopgetbatch-processingnested-loops

PHP Batch Processing For Loop


I have an end point that I can send a GET request to and it returns a different result depending on the limit and offset.

https://example-foo-bar.com?l=100&o=0 // returns the first 100 items.

I want to create a for loop (or a Nested for loop I assume) that returns a 100 items at a time, adding the result to an array each time, until the end of the response. I have the code for sending the curl request and storing the result, just struggling on the batch processing part.

Something like:

https://example-foo-bar.com?l=100&o=0

https://example-foo-bar.com?l=100&o=99

https://example-foo-bar.com?l=100&o=199

https://example-foo-bar.com?l=100&o=218 // end of response ?

I also know how many result there are in total, stored as $count;


I ended up with something like this but it doesn't feel like the best practice:

function testLoop(){

  $limit = 100;

  $count = getCount();

  $j = ceil($count/$limit);

  for ($i = 0; $i < $j; $i++){
    $offset = $i*100;
    echo 'https://example-foo-bar?l='.$limit.'&o='.$offset.'';
  }
 }

 testLoop();

Solution

  • I am not sure if I understand the question correctly. But are you looking for something like that?

    $offset = 0;
    $limit = 100;
    $run = true;
    $result_array = array();
    while($run) {      
      $result_array = array_merge($result_array, json_decode(file_get_contents("https://example-foo-bar.com?l=".$limit."&o=".$offset),true));
      $offset = $offset + $limit;
      if($offset == {somenumber}) {
        $run = false;
      }
    }
    

    Then use a cron job to call the php file