【问题标题】:PHP loop curl request one by onePHP循环curl请求一一
【发布时间】:2023-03-21 22:30:02
【问题描述】:

如何使foreachfor 循环仅在收到 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,等待请求数据,如果匹配或实现然后进入下一个循环,

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

【问题讨论】:

    标签: php loops curl for-loop foreach


    【解决方案1】:

    循环应该已经以这种方式工作,因为您使用的是阻塞 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);
    

    【讨论】:

    • 我的意思是......无论是否加载,该循环都会一次执行它们,它会这样做吗?还是等到所有条件都匹配?
    • 它将加载第一个站点或页面,并等待它成功或失败。然后它将转到下一个(在这些示例中,它始终是同一个站点)。由于我不明白您是想中断循环还是有条件地做某事,所以我就每种情况都举了一些例子。如果你只想串行而不是并行执行调用,那么代码已经可以了。
    • 所以无论有没有这些条件......它会一次运行所有循环,无论响应是什么?我的意思是,我不希望它一次运行所有这些,我希望它做一些事情并等到这件事完成然后移动到下一个循环,它已经这样做了吗?还是需要条件?
    • 这自然是你想要的方式!你不需要改变你的代码!
    • 但是如果到达会有超时,它会做其他部分并进入下一个项目
    【解决方案2】:

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

    需要考虑的几个问题

    • 您可以为curl 设置更高的超时时间,以确保没有通信延迟。 CURLOPT_CONNECTTIMEOUTCURLOPT_CONNECTTIMEOUT_MS(毫秒)、CURLOPT_DNS_CACHE_TIMEOUTCURLOPT_TIMEOUTCURLOPT_TIMEOUT_MS(毫秒)可用于增加超时。 0 使curl 无限期地等待这些超时。
    • 如果您的curl 请求因任何原因失败,您可以在此处放置exit 以停止执行,这样它就不会移动到下一个URL。
    • 如果您希望脚本在第一次失败后继续,您可以只记录结果(在失败的请求之后)并让它在循环中继续。检查日志文件将为您提供有关发生了什么的信息。

    【讨论】:

      【解决方案3】:

      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
      }
      

      【讨论】:

        【解决方案4】:

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

        foreach ($list as $thing) {
            if ($success) {
                // ...
            } else {
                break;
            }
        }
        

        【讨论】:

          【解决方案5】:
          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;
               }
           }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-02
            • 1970-01-01
            • 2012-11-05
            • 2011-03-07
            • 1970-01-01
            相关资源
            最近更新 更多