出现连接问题时处理 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 做了一些事情,因为我可以看到我的 'poop' 调试消息,但它仍然导致致命错误

这些是我得到的错误

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 页面,表明连接有问题

这是一个 cakephp 问题

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);
}