PHP 中的 SoapClient 错误回退

2021-12-24 00:00:00 php soap-client soap php-5.2

在 PHP 中,如果您尝试实例化一个新的 SoapClient,并且 WSDL 不可访问(服务器停机或其他),抛出 PHP 致命错误:

In PHP, if you try to instantiate a new SoapClient, and the WSDL is not accessible (server down or whatever), a PHP fatal error is thrown:

致命错误:SOAP-ERROR:解析 WSDL:无法加载'http://example.com/servlet/app/SomeService?wsdl': 无法加载外部实体"http://example.com/servlet/app/SomeService?wsdl">

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://example.com/servlet/app/SomeService?wsdl' : failed to load external entity "http://example.com/servlet/app/SomeService?wsdl"

PHP 中的致命错误,据我所知,无法恢复.

Fatal errors in PHP, as far as I know, are not recoverable.

有什么办法可以退缩吗?能否以某种方式避免这种致命错误?

Is there any way to fallback from this? Can this fatal error somehow be avoided?

我应该说我在 PHP 5.2 上运行,如果它有什么不同的话.

I should say that I am running on PHP 5.2, if it makes any difference.

推荐答案

这个已经讨论过了:

  • https://bugs.php.net/bug.php?id=47584 莉>
  • http://www.php.net/manual/en/class.soapclient.php#104046

Rasmus 本人提出了以下解决方案:

Rasmus himself proposed the following solution:

<?php  
try {  
    $x = @new SoapClient("non-existent.wsdl",array("exceptions" => 1));  
} catch (SoapFault $E) {  
    echo $E->faultstring; 
}  
echo "ok
";

相关文章