问题描述
我正在尝试理解 curl_multi_exec.我在这里复制了一段手动示例.所以我想知道,它是如何工作的?我猜第一个循环发送了http请求?但它之后是一个循环内的循环,使用带有看似未记录标志的函数..
I'm trying to understand curl_multi_exec. I've copied a piece of the manual example here. So I'm wondering, how does it work? The first loop sends the http request I guess? But it then it is followed by a loop inside a loop using functions with seemingly undocumented flags..
我想同时下载+=70 个网址+=.
I would like to download +=70 urls +=in parallel.
http://www.php.net/manual/en/function.curl-multi-exec.php
<?php
...
$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);
}
}
...
?>
推荐答案
您可以浏览两篇描述此示例的文章.
You can explore two article that describes this example.
首先,这是高级别的.有两个外环.第一个负责立即清除 curl 缓冲区.第二个负责等待更多信息,然后获取该信息.这是所谓的阻塞 I/O 的示例.我们阻止程序其余部分的执行,直到网络 I/O 完成.虽然这通常不是处理网络 I/O 的最佳方式,但它确实是我们在单线程、同步 PHP 中唯一的选择.
首先是 $mrc
变量,从手册中我们了解到响应是在 cURL 预定义常量中定义的 cURL 代码.本质上,它是一个常规响应,与任何其他 PHP 函数一样,curl_multi_exec
没有什么不同,只有在完成后才返回响应.这意味着应该只有一个响应.在完美的世界中,这个单一的响应是 0(零)或等于预定义的常量 CURLM_OK
.
这篇关于理解 php curl_multi_exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!