PHP loop curl request one by one
Asked Answered
B

6

8

How to make a foreach or a for loop to run only when the curl response is received..

as example :

for ($i = 1; $i <= 10; $i++) {
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_URL,"http://www.example.com");

 if(curl_exec($ch)){ // ?? - if request and data are completely received
   // ?? - go to the next loop
 }
 // DONT go to the next loop until the above data is complete or returns true
}

i don't want it to move to the next loop without having the current curl request data received.. one by one, so basically it opens up the url at first time, waits for the request data, if something matched or came true then go to the next loop,

you dont have to be bothered about the 'curl' part, i just want the loop to move one by one ( giving it a specific condition or something ) and not all at once

Bullfrog answered 6/11, 2012 at 19:16 Comment(0)
R
9

The loop ought to already work that way, for you're using the blocking cURL interface and not the cURL Multi interface.

$ch = curl_init();
for ($i = 1; $i <= 10; $i++)
{
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
    $res = curl_exec($ch);
    // Code checking $res is not false, or, if you returned the page
    // into $res, code to check $res is as expected

    // If you're here, cURL call completed. To know if successfully or not,
    // check $res or the cURL error status.

    // Removing the examples below, this code will hit always the same site
    // ten times, one after the other.

    // Example
    if (something is wrong, e.g. False === $res)
        continue; // Continue with the next iteration

    Here extra code to be executed if call was *successful*

    // A different example
    if (something is wrong)
        break; // exit the loop immediately, aborting the next iterations

    sleep(1); // Wait 1 second before retrying
}
curl_close($ch);
Redbird answered 6/11, 2012 at 19:21 Comment(6)
i mean .. will that loop execute them all at once no matter whether it loaded or not, does it do that ? or waits till all conditions are matched ?Bullfrog
It will load the first site or page, and wait until it either succeeds, or fail. Then it will go to the next (in these examples, it is always the same site). Since I did not understand if you wanted to interrupt the cycle or do something conditionally, I put some examples of each case. If you just want to execute the calls in series and not in parallel, then the code is already okay.Redbird
so with or without those conditions.. would it run all the loops in one time, no matter what the response is ? my point is, i don't want it to run all of them at once, i want it to do something and wait till this something is complete then move to the next loop, Does it already do that ? or requires a condition ?Bullfrog
it naturally is the way you want! you dont need to change your code!Hibbard
but there is a timeout if reaches it'll do the else part and goes to next itemHibbard
@Osa, it already does that. No conditions are necessary. It is true though what Morteza says, that if the loop is long enough, PHP might timeout. In that case you need to use set_time_limit. You might also want to set cURL's timeout and transfer rate options to deal with a slow or stalled line efficiently.Redbird
I
3

Your code (as is) will not move to the next iteration until the curl call is completed.

A couple of issues to consider

  • You could set a higher timeout for curl to ensure that there are no communication delays. CURLOPT_CONNECTTIMEOUT, CURLOPT_CONNECTTIMEOUT_MS (milliseconds), CURLOPT_DNS_CACHE_TIMEOUT, CURLOPT_TIMEOUT and CURLOPT_TIMEOUT_MS (milliseconds) can be used to increase the timeouts. 0 makes curl wait indefinitely for any of these timeouts.
  • If your curl request fails for whatever reason, you can just put an exit there to stop execution, This way it will not move to the next URL.
  • If you want the script to continue even after the first failure, you can just log the result (after the failed request) and let it continue in the loop. Examining the log file will give you information as to what happened.
Instantly answered 6/11, 2012 at 19:26 Comment(0)
S
2

The continue control structure should be what you are looking for:

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

http://php.net/manual/en/control-structures.continue.php

for ($i = 1; $i <= 10; $i++) {
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,"http://www.example.com");

  if(curl_exec($ch)){ // ?? - if request and data are completely received
    continue; // ?? - go to the next loop
  }
  // DONT go to the next loop until the above data is complete or returns true
}
Slug answered 6/11, 2012 at 19:22 Comment(0)
E
1

You can break out of a loop with the break keyword:

foreach ($list as $thing) {
    if ($success) {
        // ...
    } else {
        break;
    }
}
Ethicize answered 6/11, 2012 at 19:20 Comment(0)
S
0
for($i = 1; $i <= 10; $i++) {
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_URL,"http://www.example.com");

 if(curl_exec($ch)){ // ?? - if request and data are completely received
   continue;
 }else{
   break;
 }
 // DONT go to the next loop until the above data is complete or returns true
}

or

for($i = 1; $i <= 10; $i++) {
     $ch = curl_init();
     curl_setopt($ch,CURLOPT_URL,"http://www.example.com");

     if(curl_exec($ch)===false){ // ?? - if request and data are completely received
       break;
     }
 }
Scherzando answered 6/11, 2012 at 19:21 Comment(0)
C
0

This is a situation for a post-test loop. You actually want to execute the conditional loop break AFTER the body of your loop -- use do {} while().

This structure unconditionally calls the first iteration, then only continues while the $result value is truthy.

$ch = curl_init();
$i = 1;
do {
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
    $result = curl_exec($ch);
    ++$i;
} while ($result);  // add `&& $i <= 10` here, if you actually need it
Centrifugal answered 25/3 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.