理解php curl_multi_exec [英] understanding php curl_multi_exec

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

问题描述

我试图理解curl_multi_exec。我在这里复制了一个手动示例。所以我想知道,它是如何工作的?第一个循环发送http请求我猜?但是它后面是一个循环里面循环使用函数看似无证的标志..



我想下载+ = 70 urls + =并行。



http:// www .php.net / manual / en / function.curl-multi-exec.php

 <?php 
...
$ active = null;
//执行句柄
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);
}
}
...
?>


解决方案

您可以浏览两篇描述此示例的文章。 / p>

PHP和curl_multi_exec


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


Doing curl_multi_exec正确的方式


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



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..

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 and curl_multi_exec

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.

Doing curl_multi_exec the right way

First the $mrc variable and from the manual we learn that the response is a cURL code defined in the cURL Predefined Constants. In esssence 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天全站免登陆