CURL MULTI-为什么是两个循环


CURL MULTI - why two loops?

有人能帮我理解为什么这里有两个循环吗?

 <?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);
?>

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

另一个问题是,代码奇怪地将所有内容都输出到屏幕上,而在没有multi_exec功能的情况下使用curl时从未发生过这种情况。我以前需要重复一下内容,但这次不行,它甚至不问就脱口而出。

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

我相信这个示例代码源于我们在curl项目中用纯C编写的演示代码,该代码进行了第一次调用/循环,看看是否应该继续。

您可以很容易地重写代码,只使用一个循环。