理解 php curl_multi_exec [英] understanding php curl_multi_exec

查看:36
本文介绍了理解 php curl_multi_exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 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.

PHP 和 curl_multi_exec

首先,这是高级别的.有两个外环.第一个负责立即清除 curl 缓冲区.第二个负责等待更多信息,然后获取该信息.这是所谓的阻塞 I/O 的示例.我们阻止程序其余部分的执行,直到网络 I/O 完成.虽然这通常不是处理网络 I/O 的最佳方式,但它确实是我们在单线程、同步 PHP 中唯一的选择.

First, here's the high level. There are two outer loops. The first one is responsible for clearing out the curl buffer right now. The second one is responsible for waiting for more information, and then getting that information. This is an example of what is called blocking I/O. We block execution of the rest of the program until the network I/O is done. While this isn't the most preferable way in general to handle network I/O, it's really our only choice in single-threaded, synchronous PHP.

做curl_multi_exec 正确的方式

首先是 $mrc 变量,从手册中我们了解到响应是在 cURL 预定义常量中定义的 cURL 代码.本质上,它是一个常规响应,与任何其他 PHP 函数一样,curl_multi_exec 没有什么不同,只有在完成后才返回响应.这意味着应该只有一个响应.在完美的世界中,这个单一的响应是 0(零)或等于预定义的常量 CURLM_OK.

First the $mrc variable and from the manual we learn that the response is a cURL code defined in the cURL Predefined Constants. In essence it is a regular response and as with any other PHP function curl_multi_exec is no different and only returns a response once it is finished. Which means there should be only ONE response. In a perfect world this single response is 0 (zero) or equal to the predefined constant CURLM_OK.

这篇关于理解 php curl_multi_exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆