Using curl_multi_exec() in PHP to Execute Multiple cURL Requests Concurrently
This article introduces PHP’s curl_multi_exec() function, explains its prototype, parameters, return values, provides a complete example for concurrently sending multiple HTTP requests, and outlines important usage considerations for efficient backend network communication.
cURL is an open‑source library for network communication in PHP, supporting protocols such as HTTP, HTTPS, and FTP.
The curl_multi_exec() function allows simultaneous execution of multiple cURL handles, improving request efficiency.
Function Prototype
int curl_multi_exec(resource $multi_handle, int &$still_running)Parameters
$multi_handle: the multi‑handle resource created by curl_multi_init(). $still_running: variable that receives the number of handles still running.
Return Value
If an error occurs, curl_multi_exec() returns an error code; otherwise it returns 0 on success.
Example Code
<?php
// Create a multi‑handle
$multi_handle = curl_multi_init();
// Create individual handles and add them to the multi‑handle
$handles = array();
$handles[] = create_curl_handle("https://example.com/api1");
$handles[] = create_curl_handle("https://example.com/api2");
$handles[] = create_curl_handle("https://example.com/api3");
foreach ($handles as $handle) {
curl_multi_add_handle($multi_handle, $handle);
}
// Execute the handles
$running = null;
do {
curl_multi_exec($multi_handle, $running);
curl_multi_select($multi_handle); // wait for I/O
} while ($running > 0);
// Retrieve responses
foreach ($handles as $handle) {
$response = curl_multi_getcontent($handle);
echo $response;
curl_multi_remove_handle($multi_handle, $handle);
curl_close($handle);
}
// Close the multi‑handle
curl_multi_close($multi_handle);
function create_curl_handle($url) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
return $handle;
}
?>Important Notes
Call curl_multi_add_handle() for each session before invoking curl_multi_exec().
Ensure all requests have finished (running handles equal 0) before using curl_multi_getcontent().
Remove each handle with curl_multi_remove_handle() and close it with curl_close() after processing.
Conclusion
The curl_multi_exec() function is a powerful tool for concurrently executing multiple cURL sessions, boosting request efficiency; the provided example demonstrates its usage and can be adapted for various backend development scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
