Php 卷曲多-为什么是两个循环?
有人能帮我理解为什么这里有两个回路吗Php 卷曲多-为什么是两个循环?,php,curl,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
<?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);
?>
代码取自第一个示例
另一个问题是,代码奇怪地将所有内容输出到屏幕,而在没有multi_exec特性的情况下使用curl时从未发生过这种情况。我以前需要重复这些内容,但这次不是,它甚至不问一声就脱口而出了所有内容。对此没有很好的解释。它确实可以移动到单个循环中 我相信这个示例代码源于我们在curl项目中用纯C编写的演示代码,该项目进行了第一次调用/循环,以查看它是否应该继续
您可以轻松地重写代码,只使用一个循环。bcoa有两个不同的
UR
L和两个不同的curl对象
existsNo,这不是原因。多接口“驱动”同一循环中的多个并发循环。我尝试删除第一个do while循环,但它不起作用。您还需要使用一些智能,而不仅仅是删除一些行!第二个循环上的while()条件需要与它第一次到达那里时匹配,因为当然,否则它将永远不会运行超过,为什么它会将所有内容输出到屏幕?这是一个示例,您需要根据需要对其进行更改