在出现连接问题时处理 SOAP 致命错误


Handle SOAP fatal error when there is a connection issue

>我正在尝试使用 php 5.6 中的 WSDL 文件连接到 soap 服务

如果我在网络上,下面的代码段工作正常,但如果断开连接,我会收到致命错误。

try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
}
catch (SoapFault $fault) {
    echo 'poop';
}
catch (Exception $exception) {
    echo 'pee';
}

编辑:它似乎确实对 SoapFault 做了一些事情,因为我可以看到我的"便便"调试消息,但它仍然会导致致命错误

这些是我得到的错误

Warning (2): SoapClient(): php_network_get_addresses: getaddrinfo failed: No such host is known.
Warning (2): SoapClient(http://soap.service.com/serivce.svc) [soapclient.soapclient]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.
Error: SOAP-ERROR: Parsing Schema: can't import schema from 'http://soap.service.com/serivce.svc'

我如何优雅地处理错误,以便 php 继续运行,以便我可以设置一个变量并呈现一个 HTML 页面,指示连接出现问题

这是一个蛋糕问题

https://github.com/cakephp/cakephp/issues/8501

$restore = error_reporting(0);
try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
}
catch (SoapFault $e) {
    trigger_error($e->getMessage()); // Overwrites E_ERROR with E_USER_NOTICE
}
finally {
    error_reporting($restore);
}