In development, we may encounter situations where we need to initiate multiple HTTP requests at the same time. PHP's cURL library provides a very convenient way to initiate multiple HTTP requests in parallel, which is the curl_multi_* function. curl_multi_add_handle and curl_multi_close are two key functions in this type of operation. This article will explain how to use them correctly for multiple cURL request management.
cURL is a tool for sending requests to servers and getting responses. It supports multiple protocols such as HTTP, HTTPS, FTP, etc. In PHP, cURL can be operated through a series of functions, the most commonly used ones include curl_init , curl_setopt , and curl_exec .
The curl_multi_add_handle function is used to add multiple cURL session handles to a multiple cURL processing pool. This allows us to process multiple requests in parallel, thereby improving efficiency, especially when multiple HTTP requests need to be sent.
// Initialize multiple cURL Session
$ch1 = curl_init('https://gitbox.net/api/resource1');
$ch2 = curl_init('https://gitbox.net/api/resource2');
// Set up each cURL Requested options
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// How much initialization cURL Handle
$multiHandle = curl_multi_init();
// Will cURL HandleAdd to到多重 cURL Handle中
curl_multi_add_handle($multiHandle, $ch1);
curl_multi_add_handle($multiHandle, $ch2);
// Execute a request
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
// Wait for an activity request to complete
curl_multi_select($multiHandle);
}
} while ($active && $status == CURLM_OK);
// Get response data
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
// Output result
echo "Response from resource1: $response1\n";
echo "Response from resource2: $response2\n";
// Close each cURL Session
curl_multi_remove_handle($multiHandle, $ch1);
curl_multi_remove_handle($multiHandle, $ch2);
In the above code, curl_multi_add_handle adds two cURL handles $ch1 and $ch2 to the multiple cURL handle $multiHandle , and then performs these requests via curl_multi_exec . curl_multi_select will wait for the request to complete, thereby implementing parallel processing.
When all requests are completed, we need to close the multiple cURL handle. At this time, use the curl_multi_close function. This function releases resources related to the handle created by curl_multi_init .
// How much to close after completing the request cURL Handle
curl_multi_close($multiHandle);
After using curl_multi_close , all resources related to the multiple cURL handle are cleaned. Note that before closing multiple cURL handles, all added cURL handles must be removed first, otherwise resource leaks will occur.
Here is a complete example showing how to manage multiple HTTP requests using curl_multi_add_handle and curl_multi_close :
// Initialize multiple cURL Session
$ch1 = curl_init('https://gitbox.net/api/resource1');
$ch2 = curl_init('https://gitbox.net/api/resource2');
// set up cURL Options
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// How much initialization cURL Handle
$multiHandle = curl_multi_init();
// Add to cURL Session到多重处理池
curl_multi_add_handle($multiHandle, $ch1);
curl_multi_add_handle($multiHandle, $ch2);
// Perform multiple requests
do {
$status = curl_multi_exec($multiHandle, $active);
if ($active) {
curl_multi_select($multiHandle); // Wait for the request to complete
}
} while ($active && $status == CURLM_OK);
// Get the returned content
$response1 = curl_multi_getcontent($ch1);
$response2 = curl_multi_getcontent($ch2);
// Output request result
echo "Response from resource1: $response1\n";
echo "Response from resource2: $response2\n";
// Remove cURL Session
curl_multi_remove_handle($multiHandle, $ch1);
curl_multi_remove_handle($multiHandle, $ch2);
// How much is off cURL Handle
curl_multi_close($multiHandle);
This code fully demonstrates how to initiate multiple HTTP requests in parallel and close multiple cURL handles after the request is completed.