KD-*_*cer 5 php zend-framework2 guzzle6
我有一个任务来确定链接的存在。因此,我使用 guzzle 客户端来识别链接是否存在,即如果收到的响应标头是 200,则链接存在,否则不存在。
下面是我的代码片段
public function checkUrl($url) {
$result['isValid'] = false;
try {
$response = $this->client->get($url, ['verify', false]);
} catch (\Exception $ex) {
$result['isValid'] = false;
$result['message'] = 'Some error message';
return $result;
}
if ($response->getStatusCode() == Response::STATUS_CODE_200) {
$result['isValid'] = true;
}
$result['message'] = 'Success - ' . $response->getStatusCode();
$response->getBody()->close();
return $result;
}
Run Code Online (Sandbox Code Playgroud)
where在构造函数中$this->client初始化为GuzzleHttp\Client对象一次
当我运行我的脚本时,一段时间后它会抛出如下错误:
PHP Fatal error: Uncaught ErrorException: include(/project/vendor/zendframework/zend-view/src/Model/ConsoleModel.php): failed to open stream: Too many open files
当我使用命令检查打开文件的列表时lsof -p <process id> -n,我注意到有很多打开的文件是由于大量请求(即 curl 响应)而导致的,这似乎是导致此异常的原因。
对解决方案有什么建议可以关闭这些回复吗?