0

我试图以比默认 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文件中的类,但似乎找不到处理错误代码并忽略错误消息的方法。有人对另一种可能的解决方案有任何想法吗?

4

1 回答 1

1

对不起,看错了。

你可以设置error_reporting(E_ALL ^ E_NOTICE)

于 2013-06-04T19:40:51.193 回答