7

如何使一个foreach或一个for循环仅在收到 curl 响应时运行..

例如:

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
}

我不希望它在没有收到当前 curl 请求数据的情况下移动到下一个循环.. 一个接一个,所以基本上它会在第一次打开 url,等待请求数据,如果匹配或实现然后进入下一个循环,

您不必为“卷曲”部分而烦恼,我只想让循环一个一个地移动(给它一个特定的条件或其他东西)而不是一次全部移动

4

5 回答 5

8

循环应该已经以这种方式工作,因为您使用的是阻塞 cURL 接口而不是 cURL Multi 接口。

$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);
于 2012-11-06T19:21:08.047 回答
2

continue控制结构应该是您正在寻找的:

continue在循环结构中使用以跳过当前循环迭代的其余部分并在条件评估处继续执行,然后开始下一次迭代。

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
}
于 2012-11-06T19:22:47.900 回答
2

curl在调用完成之前,您的代码(原样)不会移动到下一次迭代。

需要考虑的几个问题

  • 您可以设置更高的超时时间curl以确保没有通信延迟。CURLOPT_CONNECTTIMEOUT, CURLOPT_CONNECTTIMEOUT_MS(毫秒), CURLOPT_DNS_CACHE_TIMEOUT,CURLOPT_TIMEOUTCURLOPT_TIMEOUT_MS(毫秒) 可用于增加超时。0 使curl任何这些超时都无限期地等待。
  • 如果您的curl请求由于某种原因失败,您可以放一个exit那里停止执行,这样它就不会移动到下一个 URL。
  • 如果您希望脚本在第一次失败后继续,您可以只记录结果(在失败的请求之后)并让它在循环中继续。检查日志文件将为您提供有关发生情况的信息。
于 2012-11-06T19:26:16.717 回答
1

break您可以使用以下关键字跳出循环:

foreach ($list as $thing) {
    if ($success) {
        // ...
    } else {
        break;
    }
}
于 2012-11-06T19:20:31.100 回答
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
}

或者

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;
     }
 }
于 2012-11-06T19:21:19.493 回答