In PHP programming, the curl_multi_* function provides powerful multitasking concurrent processing capabilities. However, during actual development, you may encounter some common problems when you use curl_multi_close() to cooperate with curl_setopt_array() . Today, we will help you analyze and troubleshoot these issues.
The curl_multi_* function is a tool for executing multiple concurrent requests, the most commonly used functions include:
curl_multi_init() : Initializes multiple cURL sessions.
curl_multi_add_handle() : Add a cURL handle to the multiple cURL processor.
curl_multi_exec() : executes multiple cURL requests.
curl_multi_close() : Close cURL multiple sessions.
In actual use, we usually set multiple options with curl_setopt_array() , but when an exception occurs, positioning problems may be more troublesome.
<?php
// Initialize multiple cURL Session
$multiHandle = curl_multi_init();
// Create multiple cURL Handle
$curlHandles = [];
for ($i = 0; $i < 3; $i++) {
$curlHandles[$i] = curl_init();
// set up cURL Options
curl_setopt_array($curlHandles[$i], [
CURLOPT_URL => "https://gitbox.net/api/resource" . $i, // replace URL domain name
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
// 添加Handle到多重 cURL Session中
curl_multi_add_handle($multiHandle, $curlHandles[$i]);
}
// How much execution cURL ask
$running = null;
do {
curl_multi_exec($multiHandle, $running);
} while ($running);
// 获取ask结果
foreach ($curlHandles as $handle) {
$response = curl_multi_getcontent($handle);
echo "Response: $response\n";
}
// closure cURL Handle
foreach ($curlHandles as $handle) {
curl_multi_remove_handle($multiHandle, $handle);
curl_close($handle);
}
// closure多重 cURL Session
curl_multi_close($multiHandle);
?>
Sometimes, even if the request has been completed, curl_multi_exec() still does not end, causing the program to be unable to continue execution. This problem usually occurs in the following situations:
The timeout option is not set correctly : If your request does not set the timeout option, or the timeout is set too long, curl_multi_exec() may get stuck here, causing it to be unable to exit.
curl_multi_remove_handle() or curl_close() is not called : curl_multi_exec() may also enter a dead loop if the handle is not removed and closed correctly.
Solution : Make sure you call curl_multi_remove_handle() and curl_close() to clean up the resources correctly after the processing is completed.
curl_setopt_array() is used to set multiple options at once, but sometimes cURL behavior is abnormal due to option order or invalid options. Especially during the use of curl_multi_* , common problems include:
Inconsistent URL settings : If different cURL handles use the same URL configuration, conflicts between requests may occur.
Option override issue : When curl_setopt_array() is called multiple times, the previous options will be overwritten, resulting in inconsistent configuration.
Solution : Check all options passed in curl_setopt_array() to make sure they are correct and there is no conflict. For example, check if the URL is replaced correctly.
After each call to curl_multi_init() , curl_multi_close() must be called to close multiple sessions. If you forget to call curl_multi_close() , this may lead to memory leaks or excessive file descriptor usage, which in turn leads to performance problems.
Solution : After the code is finished, make sure that curl_multi_close() is called to close the multiple cURL session.
Check URL settings : Make sure all cURL handles have valid URLs, especially when setting multiple options with curl_setopt_array() , check for errors or omissions.
Debug log : Enable cURL debug output to view the detailed execution process of the cURL handle. This helps locate whether the request was initiated successfully or where an exception occurred.
Timeout configuration : Set the appropriate timeout time and make sure that each request has a timeout setting. Avoid the request hangs for too long and the program cannot exit.
Resource Cleanup : Each cURL handle should be properly closed after use, including calls to curl_multi_remove_handle() and curl_close() .
Memory and file handles : Check PHP's memory limits and file handle limits to ensure that multiple cURL sessions do not cause resource leakage.
By using the curl_multi_* function rationally, you can efficiently handle multiple concurrent requests. However, in actual development, details problems can easily lead to exceptions or performance problems. Hopefully the troubleshooting steps provided in this article can help you solve common problems and ensure that multitasking cURL requests are executed smoothly.