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

时间:2016-03-11 22:19:21

标签: php soap soap-client

我尝试使用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页面,表明连接存在问题

2 个答案:

答案 0 :(得分:2)

这是一个蛋糕问题

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

答案 1 :(得分:0)

您应该考虑所有异常类型。尝试在catch语句中使用 Exception 而不是 SoapFault