Current Location: Home> Latest Articles> How to avoid repeated opening and closing of cURL sessions when using curl_close?

How to avoid repeated opening and closing of cURL sessions when using curl_close?

gitbox 2025-05-18

cURL is a powerful tool in PHP for URL requests. By using cURL, we can communicate with different servers, perform GET, POST requests, download files, submit form data, etc. cURL uses a session (Session) to manage requested connections and resources. Usually we initialize a session through curl_init() and close it through curl_close() .

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

In the above code, we initialize and close the cURL session every time we send a request. While this is fine for a single request, frequent session initialization and shutdown can affect performance when we need to send a large number of requests.

2. The root of the problem: frequent opening and closing cURL sessions

Each time curl_init() is called, PHP allocates new memory and initializes the relevant resources. And each time curl_close() is called, these resources will be released. Frequent execution of these operations can be particularly inefficient for multiple requests, especially in high concurrency environments, where repeated initialization and shutdowns can increase the burden on the system.

 // Initialize and close sessions multiple times
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "https://gitbox.net/api/endpoint1");
$response1 = curl_exec($ch1);
curl_close($ch1);

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://gitbox.net/api/endpoint2");
$response2 = curl_exec($ch2);
curl_close($ch2);

The above code executes curl_init() and curl_close() between each request, which wastes time and memory when handling multiple requests.

3. How to avoid repeated opening and closing of cURL sessions?

To improve code efficiency, we can use a long-connected cURL session. In this approach, we only need to initialize the cURL session once, and then dynamically change the requested URL and other parameters via curl_setopt() without reinitializing each time.

We can keep the cURL resource in a loop and only update the URL and other options before each request. This method greatly reduces the number of calls to curl_init() and curl_close() , thereby improving efficiency.

 $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

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

curl_close($ch);

In the above code, we only called curl_init() and curl_close() once, and the URL was dynamically modified through curl_setopt() . This method avoids repeated session initialization and closing, and improves code efficiency.

4. Bulk requests using cURL

If you need to send a large number of requests, PHP's curl_multi_* function can help you send multiple requests at the same time, further improving efficiency. curl_multi_init() can initialize a multiple cURL resource, allowing you to send multiple requests in parallel without frequently opening and closing separate cURL sessions.

 // initialization cURL Batch sessions
$mh = curl_multi_init();

// Create multiple cURL Session
$ch1 = curl_init("https://gitbox.net/api/endpoint1");
$ch2 = curl_init("https://gitbox.net/api/endpoint2");

curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

// 将Session添加到Batch sessions中
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2);

// Perform batch requests
do {
    $status = curl_multi_exec($mh, $active);
} while ($status == CURLM_CALL_MULTI_PERFORM || $active);

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

// closure cURL Session
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

Using the curl_multi_* function, you can send multiple requests at the same time without initializing and closing a cURL session every time. This not only improves efficiency, but also allows you to process multiple requests at the same time, greatly reducing waiting time.

5. Summary

By avoiding calling curl_init() and curl_close() in each request and using long connections or batch requests instead, we can effectively improve the execution efficiency of our code. Especially when a large number of requests are needed, using the curl_multi_* function can further speed up the entire process and reduce resource consumption.

With these methods, you can significantly optimize performance when sending HTTP requests using cURL and lay a solid foundation for efficient PHP programming.