0

成功完成后,是否可以取消 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);
4

0 回答 0