When using the curl_multi_* function library for concurrent requests in PHP, curl_multi_close is a very critical function that closes multiple cURL session resources. However, when using it, developers are prone to encounter some pitfalls, especially when handling a large number of concurrent requests. This article will analyze the common errors of curl_multi_close in depth and provide corresponding solutions.
When using multiple concurrent requests using cURL, curl_multi_close needs to be called after all requests have been executed. If all requests are not processed correctly when curl_multi_exec is called, or the session is closed without fully executing, it may result in errors or resource leakage.
Common errors:
$mh = curl_multi_init();
$ch1 = curl_init('https://gitbox.net/api/data1');
$ch2 = curl_init('https://gitbox.net/api/data2');
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
// Error demonstration:Call in advance curl_multi_close
curl_multi_close($mh); // The request has not been completed yet
Solution:
Make sure to call curl_multi_close after all requests are completed. The correct way is to use curl_multi_exec to monitor and wait for the request to complete.
$mh = curl_multi_init();
$ch1 = curl_init('https://gitbox.net/api/data1');
$ch2 = curl_init('https://gitbox.net/api/data2');
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh); // Call correctly
Each cURL request handle should be explicitly removed after the request is completed. If you forget to remove the handle after completing the request, it may cause resource leakage and affect the performance and stability of the program.
Common errors:
curl_multi_add_handle($mh, $ch1);
// Closed directly without removing the handle multi handle
curl_multi_close($mh); // May cause resource leakage
Solution:
Before calling curl_multi_close , make sure to remove each cURL handle.
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh); // Free up resources
Sometimes some requests fail when executing concurrent requests, causing some handles to remain incomplete state before calling curl_multi_close . To prevent this type of problem, be sure to check the execution status of each request.
Common errors:
$ch1 = curl_init('https://gitbox.net/api/data1');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$response1 = curl_exec($ch1); // If the request fails,curl_exec return false
if ($response1 === false) {
echo 'Error: ' . curl_error($ch1);
}
curl_multi_close($mh); // Close early in case of error
Solution:
Make sure that each request is executed successfully before cleaning up to avoid closing the connection in advance when the request fails.
$response1 = curl_exec($ch1);
if ($response1 === false) {
echo 'Error: ' . curl_error($ch1);
} else {
curl_multi_remove_handle($mh, $ch1);
}
curl_multi_close($mh); // Make sure to close only after all requests are completed
In concurrent requests, multiple requests are issued at the same time. If the URL of one of the requests is incorrect or the request timed out, curl_multi_exec may not be able to complete all requests normally, which will affect the execution of curl_multi_close .
Common errors:
$ch = curl_init('https://gitbox.net/api/invalid_url'); // Wrong URL
curl_multi_add_handle($mh, $ch);
curl_multi_exec($mh, $active);
curl_multi_close($mh); // The request may not be completed yet
Solution:
Make sure the URL is correct and set a reasonable timeout. The time for request timeout can be set through CURLOPT_TIMEOUT .
curl_setopt($ch1, CURLOPT_URL, 'https://gitbox.net/api/data1');
curl_setopt($ch1, CURLOPT_TIMEOUT, 30); // Set the timeout to 30 Second
curl_multi_close should be called only once at the end of the life cycle of each multi-handle. Multiple calls to the function may result in unnecessary errors or resource release failures.
Common errors:
curl_multi_close($mh); // Multiple calls
curl_multi_close($mh); // The second call
Solution:
Make sure curl_multi_close is called only once and executed after all requests are completed and all handles have been removed correctly.
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh); // Called only once
Summarize
curl_multi_close is an important function in concurrent requests, but you need to pay attention to order and resource management when using it. Before calling curl_multi_close , make sure that all requests have been successfully executed and the handle is removed to avoid resource leakage or request exceptions. At the same time, through reasonable error handling and timeout control, common errors can be avoided. Mastering these techniques can make your concurrent requests more stable and efficient.