Detailed explanation of the definition and usage of PHP curl_multi_exec function

巴扎黑
Release: 2023-03-07 21:46:02
Original
2277 people have browsed it

This article explains the definition and usage examples of the PHP curl_multi_exec function. The meaning of the curl_multi_exec function is to run the sub-connection of the current cURL handle. Let’s look at its description and usage examples in detail.

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

curl_multi_exec function parameters

mh

The cURL multiple handles returned by curl_multi_init().

still_running

A reference to an identifier used to determine whether the operation is still being executed.

curl_multi_exec function return value

A cURL code defined in cURL predefined constants.

Note: This function only returns errors related to the entire batch stack. There may still be problems with individual transfers even when CURLM_OK is returned.

curl_multi_exec function example

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);

?>
Copy after login

The above is the detailed content of Detailed explanation of the definition and usage of PHP curl_multi_exec function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!