gpt4 book ai didi

php - CURL MULTI - 为什么有两个循环?

转载 作者:可可西里 更新时间:2023-11-01 00:16:17 26 4
gpt4 key购买 nike

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

 <?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 时从未发生过。之前我需要附和内容,但这次不是,它什么都不问就脱口而出。

最佳答案

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

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

您可以轻松重写代码以仅使用一个循环。

关于php - CURL MULTI - 为什么有两个循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8724607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com