0

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();
2
  • “until the end of the file” - what file? Commented Aug 20, 2018 at 10:37
  • End of response even Commented Aug 20, 2018 at 10:45

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Result is a Json object
I updated the answer. Please check if that does what you want.
If you are happy, please markl it as the correct answer :-)
0
  1. Create table 'schedule' and store the data id, link_name,offset and status column
  2. set cron to execute every 10 minutes and take First an entry (one) which status =0
  3. pass param to testLoop($limit) to call function. It may entire link of only offset =0, offset =99, offset =199 like that
  4. After completed to update status=1 in schedule table.
  5. After 10 minute cron Call Step1.

Comments

0

Best way to use Cron for such type of batch process you can also use php-resque

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.