2012-11-06 32 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。
  • 如果您希望腳本在第一次失敗後繼續運行,您可以只記錄結果(失敗的請求之後)並讓它在循環中繼續。檢查日誌文件會爲您提供有關發生的事情的信息。