Current Location: Home> Latest Articles> How to avoid the problem caused by multiple calls to curl_close when repeated requests?

How to avoid the problem caused by multiple calls to curl_close when repeated requests?

gitbox 2025-05-18

When using cURL, we first need to create a cURL session and use curl_init() to initialize a cURL handle. This handle holds the connection related information during the request process. When the request is complete, we usually use curl_close() to close the handle and free the system resources.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/data"); // Set up a request URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set the return result to a string

$response = curl_exec($ch); // Execute a request and get a response

curl_close($ch); // Close the session

In the above code, a new cURL session is created every time a request is initiated, and curl_close() is called after the request is completed to close the session. This is necessary because each cURL session will take up a certain amount of resources, and curl_close() can be used to free these resources.

2. Avoid repeated calls to curl_close() in multiple requests

In actual development, there may be scenarios where multiple requests need to be executed. If curl_close() is called immediately after each request is completed, it will cause the connection to be closed repeatedly every time, wasting system resources. To avoid this, it is usually a way to multiplex cURL sessions, sharing a session handle between multiple requests until all requests are completed before closing the session uniformly.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set the return result to a string

// ask 1
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/endpoint1");
$response1 = curl_exec($ch);

// ask 2
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/endpoint2");
$response2 = curl_exec($ch);

// ask 3
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/endpoint3");
$response3 = curl_exec($ch);

curl_close($ch); // 一次性Close the session

In this example, all requests share a cURL session handle $ch and curl_close() is called only after all requests are completed. This avoids repeated creation and destruction of sessions, thereby reducing unnecessary resource overhead.

3. Use curl_multi_* function to batch process multiple requests

If you need to initiate multiple requests at the same time, using the curl_multi_* function to process multiple cURL sessions in parallel is a more efficient way. In this way, you can avoid multiple calls to curl_close() and better manage multiple requested resources.

 $mh = curl_multi_init(); // Initialize multiple handles
$ch1 = curl_init();
$ch2 = curl_init();

curl_setopt($ch1, CURLOPT_URL, "https://gitbox.net/api/endpoint1");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch2, CURLOPT_URL, "https://gitbox.net/api/endpoint2");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

curl_multi_add_handle($mh, $ch1); // Add a handle to multiple handles
curl_multi_add_handle($mh, $ch2);

do {
    curl_multi_exec($mh, $active); // 执行ask
} while ($active > 0);

$response1 = curl_multi_getcontent($ch1); // Get a response
$response2 = curl_multi_getcontent($ch2);

curl_multi_remove_handle($mh, $ch1); // Remove handle from multiple handles
curl_multi_remove_handle($mh, $ch2);

curl_close($ch1); // Close the handle
curl_close($ch2);
curl_multi_close($mh); // Close multiple handles

In this approach, multiple cURL sessions are executed in parallel via the curl_multi_* function. Each requested resource is closed only once after completion, thereby improving the execution efficiency of the program and ensuring that each requested resource is properly released.

4. Error handling and resource release

Although using curl_close() correctly can avoid resource leakage, you may also encounter request failures when executing cURL requests. If an error occurs, it is critical to ensure that the resource is properly freed with each request. You can check curl_errno() to determine whether the request is successful. If it fails, the corresponding error processing will be performed.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/invalid-endpoint");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if(curl_errno($ch)) {
    // ask失败时的处理
    echo "cURL Error: " . curl_error($ch);
} else {
    // ask成功时的处理
    echo "Response: " . $response;
}

curl_close($ch); // 即使ask失败,Also ensure the release of resources

5. Summary

Correct use of curl_close() function can effectively release resources occupied by cURL sessions and avoid performance problems caused by repeated calls. Sharing the same session between multiple requests, or using the curl_multi_* function for parallel processing can reduce the frequency of multiple calls to curl_close() , thereby improving program execution efficiency and resource management. In addition, error handling and resource release are equally important, ensuring that resources are properly freed with each request, avoiding memory leaks or other issues.