Sebastián Grignoli Sebastián Grignoli - 1 month ago 11
PHP Question

PHP not so fatal errors with "new SoapClient()"

I have this snippet running:

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.
}
}


When I run the program the Web Service URL responds me with an authentication error (which is ok at this point of development).

The extrange behavior I'm noting is that, while I was expecting for this:

$ 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"


It's giving me this:

$ 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"


Why is a "PHP Fatal error" not killing the program? And why is it escaping the try/catch block?

How can I avoid this?

Answer

I had the same problem and found a solution at https://bugs.php.net/bug.php?id=47584.

At first you should set exceptions options to force SoapClient to throw Exceptions:

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

in my case xdebug was forcing to produce an Fatal Error instead of a catchable Exception. So you should try to disable xdebug for the SoapClient creation:

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

hope this will help you ^^

Comments