In PHP, the curl_multi_* series of functions are used to execute concurrent cURL requests, which can help us handle multiple HTTP requests simultaneously. When we complete the concurrent request, we need to use curl_multi_close to close the cURL handle, which is usually done after all concurrent requests are executed. In this article, we will explore how to use curl_multi_close , common errors, and how to resolve them.
The curl_multi_close function is used to close the cURL multi-handle initialized by curl_multi_init . Its function is to clean up related resources and avoid memory leaks. When using it, we usually use curl_multi_init to initialize a multiple cURL handle, and call curl_multi_close after all requests are executed to close the handle.
<?php
// initializationcURLMultiple handles
$mh = curl_multi_init();
// initializationcURLSingle handle
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://gitbox.net/api/v1/data1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://gitbox.net/api/v1/data2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// 将Handle添加到Multiple handles中
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
// Execute multiplecURLask
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
// closurecURLHandle
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
// closure单个cURLHandle
curl_close($ch1);
curl_close($ch2);
echo "ask完成!";
?>
In the example above, curl_multi_init initializes a multi-handle $mh , and then we create two separate cURL handles $ch1 and $ch2 . These two handles are added to $mh via curl_multi_add_handle and multiple HTTP requests are performed. Finally, use curl_multi_remove_handle to remove these handles, and call curl_multi_close to close the multiple handles and clean up the relevant resources.
Although curl_multi_close is simple, you will also encounter some common problems and errors when using it with curl_multi_init . Next, we will discuss these common errors and their solutions.
Error description: Sometimes we may forget to call curl_multi_init to initialize multiple handles and start adding handles directly.
Workaround: Make sure that the multiple handles have been properly initialized before calling curl_multi_add_handle . Also remember to use curl_multi_close to clean it up at the end.
<?php
$mh = curl_multi_init(); // 确保已经initialization
// Other codes...
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);
// 最后closure
curl_multi_close($mh);
?>
Error description: Resource leaks may occur if curl_multi_remove_handle is not properly called after all requests are completed, or if curl_multi_close is not used to close multiple handles.
Workaround: Make sure that each individual cURL handle added to the multiple handle is removed and curl_multi_close is called at the end.
<?php
// 确保移除所有Handle
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh); // Clean up resources
?>
Error description: When executing multiple cURL requests, if curl_multi_close does not wait for all requests to complete, you may encounter the problem that the request is not fully executed.
Workaround: Use curl_multi_exec loop to ensure that all requests are completed. curl_multi_exec will be executed until all requests are completed.
<?php
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0); // 等待所有ask完成
curl_multi_close($mh); // closureMultiple handles
?>
curl_multi_close is a step that cannot be ignored when handling multiple cURL requests. It ensures that after the concurrent request is completed, the relevant resources can be released. When used with curl_multi_init , common errors mainly focus on initialization and resource cleaning. Most errors can be avoided by carefully checking the code and ensuring that each step is executed correctly.
Hopefully this article helps you better understand and use curl_multi_close and curl_multi_init . If you have more questions, please leave a message to discuss!