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

标签 php curl

我在 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/

相关文章:

java - 如何调用仅接受字符串值作为请求正文参数的端点

php - 我想在 php 中 curl 谷歌搜索结果

php - 多维数组,去掉key和value匹配到另一个数组的数组

PHP 类型提示可调用函数

java - OpenFigi API curl 到 java POST

hadoop - LAN 虚拟机上的 Web 应用程序 : curl -L works from other vms, 浏览器/主机上的 curl 没有

c++ - curl 在线程调用中崩溃

php - 通过更改从函数返回的引用来更改全局数组值

php - sql UPDATES 更新更改列之后的行中的每一列

PHP session 未在终端中正确显示