gpt4 book ai didi

php - phpseclib : how to handle connection error gracefully when instantiating a New_SFTP object

转载 作者:行者123 更新时间:2023-12-03 08:33:22 46 4
gpt4 key购买 nike

我正在尝试以比默认API更优美的方式处理此连接错误。默认的API将显示一条错误消息,但是我想抑制它并以自己的方式替换它,识别错误并进行处理,但是我不确定该怎么做。

public function __construct($host)
{
// trying to test by opening a failed connection -- try catch
// block doesn't work because no exception thrown
$this->connection = new Net_SFTP("nonexistent.domain.com");

// this will not work either ....
if ( !$this->connection->status_codes )
{
throw new Exception("Could not create SFTP connection.\n");
}

else
{
echo "Connected via SFTP to " . $host . " on port 22.\n";
}
}

执行它将显示,即使有错误,也会击中else块:
PHP Notice:  Cannot connect to nonexistent.domain.com. Error 0. php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/dylan/Documents/php/Net/SSH2.php on line 788
Connected via SFTP to gluon.exavault.com on port 22.

我一直在看 Net/SFTP.php文件中的类,但是我似乎找不到找到处理错误代码并忽略错误消息的方法。是否有人对其他可能的解决方案有任何想法?

最佳答案

对不起,看错了。

您可以设置error_reporting(E_ALL ^ E_NOTICE)

关于php - phpseclib : how to handle connection error gracefully when instantiating a New_SFTP object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926032/

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