1 つが正常に完了したときに、curl_multi で実行中のすべての呼び出しをキャンセルすることは可能ですか? curl_multi はスクリプトを終了する前にすべてのプロセスが終了するまで待機しているように見えますが、これは私の個人的な認識である可能性があります。多くの接続が開いている単一の URL を呼び出そうとしています。URL は要求をできるだけ速く処理し、最初の要求が戻ってきたら、残りの接続を閉じて、残りの呼び出しスクリプトを続行します。
$outArr = array();
$master = curl_multi_init();
$curl_arr = array();
$url = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on') ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$std_options = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5);
for ($i = 0; $i < 30; $i++) {
//set custom URL so we can see which one completed.
$std_options[CURLOPT_URL] = $url.'/sub_proc.php?somearg='.$somearg.'&tlimit='.$tlimit.'&i='.$i;
$ch = curl_init();
curl_setopt_array($ch,$std_options);
curl_multi_add_handle($master, $ch);
}
do {
while(($execrun = curl_multi_exec($master, $running)) == CURLM_CALL_MULTI_PERFORM);
if($execrun != CURLM_OK)
break;
// a request was just completed -- find out which one
while($done = curl_multi_info_read($master)) {
$info = curl_getinfo($done['handle']);
if ($info['http_code'] == 200) {
$output = curl_multi_getcontent($done['handle']);
// request successful. process output using the callback function.
$out = json_decode($output);
$outArr = $out['board'];
$running = false;
break;
//I want the code above to break all processing requests and
//continue with the script outside of this loop.
} else {
// request failed. add error handling.
}
}
} while ($running);
curl_multi_close($master);
//does not execute until all requests are complete,
//causing timeouts when one failed request takes > 30 seconds
print_r($outArr);