In development, we often need to initiate HTTP requests to multiple servers or APIs. If you request one by one according to the traditional single-threading method, it will not only be slow, but also waste server resources. PHP provides cURL extension, where curl_multi_exec and curl_multi_close functions can allow us to implement multi-request concurrent processing, greatly improving efficiency.
This article will introduce in detail how to use curl_exec combined with curl_multi_close for efficient concurrency processing, and give specific example code.
curl_exec() : executes a single cURL request.
curl_multi_exec() : Execute multiple cURL handles simultaneously.
curl_multi_close() : Close a cURL batch handle and release the resource.
If you need to process a large number of requests concurrently, it is recommended to use curl_multi_exec instead of calling curl_exec separately for each request. curl_multi_close is an important step to clean up resources after all requests are processed.
Here is a simple example that demonstrates how to request multiple interfaces concurrently and process responses:
<?php
// To requestURLList
$urls = [
"https://gitbox.net/api/data1",
"https://gitbox.net/api/data2",
"https://gitbox.net/api/data3",
];
// Initialize multiplecURLHandle
$multiHandle = curl_multi_init();
$curlHandles = [];
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($multiHandle, $ch);
$curlHandles[] = $ch;
}
// Perform all requests
$running = null;
do {
$status = curl_multi_exec($multiHandle, $running);
if ($status > 0) {
// An error occurred
echo "cURL error: " . curl_multi_strerror($status);
break;
}
// Waiting for an active connection
curl_multi_select($multiHandle);
} while ($running > 0);
// Process the response of each request
foreach ($curlHandles as $ch) {
$response = curl_multi_getcontent($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch)) {
echo 'Request Error: ' . curl_error($ch) . PHP_EOL;
} else {
echo 'HTTP Code: ' . $info['http_code'] . PHP_EOL;
echo 'Response: ' . $response . PHP_EOL;
}
curl_multi_remove_handle($multiHandle, $ch);
curl_close($ch);
}
// closuremulti handle
curl_multi_close($multiHandle);
?>
First create a multi-handle via curl_multi_init() .
After setting each cURL separately, add it to the multi handle via curl_multi_add_handle() .
Use curl_multi_exec() to execute all requests, polling until all requests are completed.
curl_multi_select() can reduce CPU idleness and improve efficiency.
After the request is completed, use curl_multi_getcontent() to obtain the response content.
Finally, remember to use curl_multi_remove_handle() to remove the handle and close the multi handle with curl_multi_close() to free up the resource.
Number of concurrent : If there are too many URLs for one-time requests, it is recommended to process them in batches, such as only 10 requests are sent at a time to avoid consuming too many resources.
Timeout handling : CURLOPT_TIMEOUT or CURLOPT_CONNECTTIMEOUT can be set to avoid long-term hangs of some requests.
Error handling : Use curl_errno() and curl_error() to check whether a single request is error-free.
Connection reuse : Turning on CURLOPT_FORBID_REUSE can avoid frequent TCP connection creation and improve performance.
By combining curl_exec and curl_multi_close , we can efficiently handle large numbers of HTTP concurrent requests in PHP. This method not only shortens the overall request time, but also makes full use of the concurrency capabilities of the server. It is one of the essential skills for building high-performance PHP applications.
By mastering these basics, you can make your PHP application run faster and more stable!