2

我有这个片段运行:

foreach($config as $wsInfo){
  try{
    $soapClient = new SoapClient($wsInfo['url'], 
                                 array('encoding'=>'ISO-8859-1'));

    //  Some more code that I commented out.

  }
  catch(Exception $e){
    echo "EXCEPTION: \n" . $e->getMessage();
    // log it, etc.
  }
}

当我运行该程序时,Web 服务 URL 会以身份验证错误响应我(在开发阶段这是可以的)。

我注意到的异常行为是,虽然我对此有所期待:

$ php scan.php -p=/ -c=config.yml
EXCEPTION: 
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"
EXCEPTION: 
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"

它给了我这个:

$ php scan.php -p=/ -c=config.yml
PHP Fatal error:  SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"
 in /home/me/project/DFPushSOAP.php on line 34
EXCEPTION: 
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"
PHP Fatal error:  SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"
 in /home/me/project/DFPushSOAP.php on line 34
EXCEPTION: 
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://webservices.myserver.com/api.asmx?WSDL' : failed to load external entity "http://webservices.myserver.com/api.asmx?WSDL"

为什么“PHP 致命错误”没有杀死程序?为什么它会逃避 try/catch 块?

我怎样才能避免这种情况?

4

2 回答 2

3

我遇到了同样的问题,并在https://bugs.php.net/bug.php?id=47584找到了解决方案。

首先,您应该设置exceptions选项以强制 SoapClient 抛出异常:

    $soapClient = new SoapClient($wsInfo['url'], array('encoding'=>'ISO-8859-1'
                                                       'exceptions' => true ));

在我的情况下,xdebug 强制产生致命错误而不是可捕获的异常。因此,您应该尝试为 SoapClient 创建禁用 xdebug:

    if(function_exists('xdebug_disable')){ xdebug_disable(); };
    $soapClient = new SoapClient($wsInfo['url'], array('encoding'=>'ISO-8859-1'
                                                       'exceptions' => true ));
    if(function_exists('xdebug_enable')){ xdebug_enable(); };

希望这对你有帮助^^

于 2013-03-26T08:54:48.230 回答
0

暂时禁用 xdebug 的解决方案对我不起作用(运行PHP 7.0.12 / amd64)并启用 xdebug)。

https://bugs.php.net/bug.php?id=47584中提到的错误的答案显示了带有自定义临时错误处理程序的解决方案。请参阅 _ [2012-10-03 09:36 UTC] james dot silver at computerminds dot co dot uk_ 的帖子。

作为一种肮脏的解决方法,您可以停用error_reporting()

$level = error_reporting();
error_reporting(0);
$soapClient = new SoapClient($wsInfo['url'], array(
    'encoding'=>'ISO-8859-1'
    'exceptions' => true )
);
error_reporting($level);

但是自定义错误处理程序看起来更令人愉快。见https://stackoverflow.com/a/12565073/4351778

于 2016-10-26T09:54:11.597 回答