7

foreachcurl 応答が受信された場合にのみ実行されるまたはループを作成する方法for..

例として:

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リクエストデータを1つずつ受信せずに次のループに移動したくないので、基本的には最初にURLを開き、リクエストデータを待ちます。次のループに進み、

「curl」部分について気にする必要はありません。ループを 1 つずつ (特定の条件または何かを指定して) 移動させたいだけで、一度にすべてではありません。

4

5 に答える 5

8

cURL Multi インターフェースではなくブロッキング cURL インターフェースを使用しているため、ループはすでにそのように機能しているはずです。

$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_CONNECTTIMEOUTCURLOPT_CONNECTTIMEOUT_MS(ミリ秒)、CURLOPT_DNS_CACHE_TIMEOUTCURLOPT_TIMEOUTおよびCURLOPT_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 に答える