In PHP, cURL creates a resource through the curl_init function to represent an ongoing HTTP request. You can set the request parameters, such as URL, request method, request header, etc., and then use curl_exec to send the request and get a response. Each cURL session will be assigned a resource handle. This handle needs to be explicitly closed after the request is completed, otherwise it will occupy system resources and may lead to memory leakage.
$ch = curl_init('https://gitbox.net/some-api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
The curl_close function is used to close a cURL session and to free all resources related to that session. After curl_exec has completed the request, the resource should be closed immediately. If curl_close is not called, PHP will retain cURL session resources, waste memory, and may lead to system performance degradation.
// Wrong way:Not closed cURL resource
$ch = curl_init('https://gitbox.net/some-api');
$response = curl_exec($ch);
// No call curl_close,resource未释放
Typically, the cURL session resource is no longer needed after sending the request, so you should close it as early as possible. Frequently creating and destroying cURL sessions is a common programming pattern, but curl_close should be called after each request is completed to avoid unnecessary memory footprint.
// The correct way to do it:Close it in time cURL resource
$ch = curl_init('https://gitbox.net/some-api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch); // closureresource,Free memory
In some complex scenarios, the program may issue multiple cURL requests, and if you do not handle it carefully, you may accidentally create and close the same resource repeatedly. This can lead to resource waste and performance issues. To avoid duplicate calls, consider using singleton pattern or some caching mechanisms to manage cURL session resources.
class CurlManager {
private $ch;
public function __construct() {
$this->ch = curl_init('https://gitbox.net/some-api');
}
public function getResponse() {
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($this->ch);
}
public function close() {
curl_close($this->ch); // resource管理
}
}
// use CurlManager Make a request
$manager = new CurlManager();
$response = $manager->getResponse();
$manager->close(); // closure cURL resource
Use management classes to avoid repeated calls and effectively control the creation and closing of resources.
Sometimes we need to send multiple cURL requests at the same time, in this case it is very important to make sure each request is closed correctly. A common practice is to save the cURL handles of multiple requests to an array and close in batches after all requests are completed.
$urls = [
'https://gitbox.net/api/endpoint1',
'https://gitbox.net/api/endpoint2',
'https://gitbox.net/api/endpoint3',
];
$multiCurl = [];
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
$multiCurl[$i] = curl_init($url);
curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $multiCurl[$i]);
}
do {
$status = curl_multi_exec($mh, $active);
if ($active) {
curl_multi_select($mh);
}
} while ($active && $status == CURLM_OK);
foreach ($multiCurl as $ch) {
curl_multi_remove_handle($mh, $ch);
curl_close($ch); // 批量closure每个resource
}
curl_multi_close($mh);
In this example, we send multiple requests simultaneously through the curl_multi_* series function, and batch close each cURL resource after all requests are completed to avoid resource leakage.
Using curl_close rationally to close cURL resources is the key to writing efficient and stable PHP programs. Whether it is a single request or a batch request, always ensure that resources are closed in time after the request is completed, memory is freed, and duplicate calls and waste of system resources are avoided. By following these best practices, you can effectively improve the performance of your PHP program and reduce memory usage, thus maintaining your application efficiency and stability.