gpt4 book ai didi

php - 是否需要关闭 cURL 连接?

转载 作者:可可西里 更新时间:2023-11-01 13:13:19 25 4
gpt4 key购买 nike

我在 PHP 中为 cURL 创建了一个包装器函数。它的简化版本如下所示:

function curl_get_contents($url, $try = 1) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, '1');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '1');

// Execute the curl session
$output = curl_exec($ch);

if ($output === FALSE) {
if ($try == 1) { //Try again
return $this->curl_get_contents($url, 2);
} else {
return false;
}
}
}

如您所见,如果函数失败,我会强制重试该函数。我需要运行 curl_close() 吗? PHP 会在脚本结束时关闭所有句柄吗?

更新

链接的问题在其答案中非常模糊,并且不支持自己的数据。我真的很感激基于可能显示 PHP 立即关闭连接的分析器的答案。

最佳答案

不,你不需要 - 垃圾处理程序会为你处理它,你不需要费心自己清理内存。

手册条目中描述了准确的答案"Resources" :

Freeing resources

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector. See the persistent connections section for more information.

关于php - 是否需要关闭 cURL 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22573339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com