Backend Development 5 min read

Using curl_multi_add_handle() in PHP to Manage Multiple cURL Handles

This article explains how the PHP curl_multi_add_handle() function can be used to add multiple cURL handles to a single session, improving network request efficiency and code maintainability, and provides a complete example with detailed code walkthrough.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using curl_multi_add_handle() in PHP to Manage Multiple cURL Handles

In PHP network request development, the curl library is a commonly used tool that provides many functions for performing various HTTP requests such as GET and POST, setting request headers, and handling cookies.

When sending multiple requests simultaneously, creating a separate cURL handle for each request can waste resources; the curl_multi_add_handle() function allows you to add multiple cURL handles to one session for efficient management.

The syntax of curl_multi_add_handle() is:

resource curl_multi_add_handle ( resource $mh , resource $ch )

The parameter $mh is an initialized cURL multi‑handle, and $ch is the new cURL handle to be added; the function returns a cURL multi handle after adding $ch to $mh .

Below is a complete example demonstrating the use of curl_multi_add_handle() :

<?php
$mh = curl_multi_init();  // initialize multi handle

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://www.example.com/api1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch1);  // add first handle

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://www.example.com/api2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $ch2);  // add second handle

// execute until all requests are complete
do {
    $status = curl_multi_exec($mh, $running);
} while ($status === CURLM_CALL_MULTI_PERFORM || $running);

$response1 = curl_multi_getcontent($ch1);
echo "Response 1: " . $response1 . "\n";

$response2 = curl_multi_getcontent($ch2);
echo "Response 2: " . $response2 . "\n";

curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>

The example first initializes a multi‑handle with curl_multi_init() , creates two independent cURL handles ( $ch1 and $ch2 ) with their URLs and options, adds them to the multi‑handle, and then repeatedly calls curl_multi_exec() until all requests finish.

After execution, curl_multi_getcontent() retrieves each response, which is then printed. Finally, the handles are removed with curl_multi_remove_handle() and the multi‑handle is closed with curl_multi_close() .

Using curl_multi_add_handle() efficiently manages multiple cURL sessions, improving request performance and code maintainability; in practice, it can be combined with curl_multi_remove_handle() and curl_multi_close() for more complex networking tasks.

In summary, the curl_multi_add_handle() function is a valuable tool for adding multiple cURL handles to a single session, thereby enhancing network request efficiency and code maintainability in PHP development.

backendPHPnetworkingcurlHTTP RequestsMulti-Handle
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.