CURL MULTI - 为什么两个循环?

Imr*_*hsh 3 php curl

任何人都可以帮我理解为什么这里有两个循环?

 <?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>
Run Code Online (Sandbox Code Playgroud)

代码来自http://php.net/manual/en/function.curl-multi-exec.php-第一个例子

另一个问题是代码奇怪地将所有内容输出到屏幕,这在使用没有multi_exec功能的curl时从未发生过.我之前需要回应这些内容,但不是为了这个内容,它甚至在没有询问的情况下脱口而出.

Dan*_*erg 8

对此没有好的解释.它确实可以移动到一个循环中.

我相信这个示例代码源自我们在curl项目中在普通C中所做的演示代码,它使第一个调用/循环看看它是否应该继续.

您可以轻松地重写代码以仅使用单个循环.

  • 那么你需要使用一些智能,而不仅仅是删除一些行!第二个循环的while()条件需要匹配它第一次到达那里,否则它将永远不会运行 (2认同)