curl_multi_add_handle

(PHP 5, PHP 7)

curl_multi_add_handle向curl批处理会话中添加单独的curl句柄

说明

int curl_multi_add_handle ( resource $mh , resource $ch )

增加 ch 句柄到批处理会话mh

参数

mh

curl_multi_init() 返回的 cURL 多个句柄。

ch

curl_init() 返回的 cURL 句柄。

返回值

成功时返回0,失败时返回CURLM_XXX之一的错误码。

范例

Example #1 curl_multi_add_handle() example

这个范例将会创建2个cURL句柄,把它们加到批处理句柄,然后并行地运行它们。

<?php
// 创建一对cURL资源
$ch1 curl_init();
$ch2 curl_init();

// 设置URL和相应的选项
curl_setopt($ch1CURLOPT_URL"http://www.example.com/");
curl_setopt($ch1CURLOPT_HEADER0);
curl_setopt($ch2CURLOPT_URL"http://www.php.net/");
curl_setopt($ch2CURLOPT_HEADER0);

// 创建批处理cURL句柄
$mh curl_multi_init();

// 增加2个句柄
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$running=null;
// 执行批处理句柄
do {
    
curl_multi_exec($mh,$running);
} while(
$running 0);

// 关闭全部句柄
curl_multi_remove_handle($mh$ch1);
curl_multi_remove_handle($mh$ch2);
curl_multi_close($mh);
?>

参见

add a note add a note

User Contributed Notes 1 note

up
4
Anonymous
4 years ago
The 'do' loop in this example will cycle indefinitely until all curl downloads complete, needlessly using CPU cycles.  A much better way is to block while dowloads are in progress.  Refer to the example code on the curl_multi_exec() page instead, which does this!
To Top