PHP curl_multi_exec Function
(PHP 5)
curl_multi_exec — Run the sub-connections of the current cURL handle
Description
int curl_multi_exec ( resource $mh , int &$still_running )
Process each handle in the stack. This method can be called whether the handle needs to read or write data.
Parameters
mh
A cURL multi handle returned by curl_multi_init().
still_running
A reference to a flag that determines whether the operation is still being executed.
Return Values
A cURL code defined in the cURL predefined constants.
Note:This function only returns errors related to the entire batch stack. Even when CURLM_OK is returned, individual transfers may still have problems.
Examples
This example will create 2 cURL handles, add them to the batch handle, and then run them in parallel.
<?php
// 创建一对cURL资源
$ch1 = curl_init();
$ch2 = curl_init();
// 设置URL和相应的选项
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);
// 创建批处理cURL句柄
$mh = curl_multi_init();
// 增加2个句柄
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$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);
}
}
// 关闭全部句柄
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
Other Extensions
PHP cURL Reference Manual