2012-11-06 31 views
3

如何使foreachfor回路接收到响应卷曲只有当运行..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 
    // ?? - go to the next loop 
} 
// DONT go to the next loop until the above data is complete or returns true 
} 

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

你不必麻烦编关于“卷曲”的一部分,我只想循环由一个(给它一个特定条件或别的东西)来移动一个,而不是一次全部

回答

1

你可以打破与break关键词循环的:

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

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

我的意思是..该循环会立即执行它们,无论它是否加载或不加载,它会这样做吗?或等到所有条件匹配? – Osa

+0

它将加载第一个站点或页面,并等待它成功或失败。然后它会转到下一个(在这些例子中,它总是一样的网站)。由于我不明白你是否想要打断周期或有条件地做某些事情,所以我列举了每个案例的一些例子。如果你只是想串联而不是并行执行呼叫,那么代码已经可以。 – LSerni

+0

所以有或没有这些条件..它会一次运行所有的循环,不管响应是什么?我的观点是,我不希望它立即运行所有这些,我希望它做某件事,然后等待这件事完成,然后转到下一个循环,它已经做到了吗?或需要一个条件? – Osa

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; 
    } 
} 
2

continue控制结构应该是你在找什么:

continue是内循环结构用于跳过当前循环的其余部分迭代并在条件评估中继续执行,然后在下一次迭代开始。直到curl调用完成

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 
} 
1

您的代码(如)将不会移动到下一个迭代。

有几个问题需要考虑

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