Yii2: SoapFault exception cannot be caught when using SoapClient

Created on 17 Jun 2016  ·  11Comments  ·  Source: yiisoft/yii2

What steps will reproduce the problem?

i want working with SoapClient in yii2 project. but i need manage error with try-catch. but that not work !

yii2 code example :

class SiteController extends Controller
{
    public function actionIndex()
    {
        try{
            new \SoapClient('a',['cache_wsdl' =>  WSDL_CACHE_MEMORY]);
        }catch(\SoapFault $e){
            echo '***************';
        }
    }
}

yii framework output :

PHP Fatal Error – yiibaseErrorException SOAP-ERROR: Parsing WSDL: Couldn't load from 'a' : failed to load external entity "a"

but in test.php(no yii framework) :

try{
    new SoapClient('a',['cache_wsdl' =>  WSDL_CACHE_MEMORY]);
}catch(SoapFault $e){
    echo '***************';
}

output

***************

stackoverflow : Yii2 - fatal error , try-catch not working

What is the expected result?

print *****

What do you get instead?

catch block not run !

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.8 |
| PHP version | 5.6.3 |
| Operating system | win 8.1 |

Most helpful comment

Seem to be already reported: https://bugs.php.net/bug.php?id=70469 @cebe, @abolfazl-ziaratban please vote on it.

Since we can't affect this behavior from within Yii, I'm closing the issue.

All 11 comments

new SoapClient('a',['cache_wsdl' => WSDL_CACHE_MEMORY]); triggers a PHP fatal error and does not throw SoapFault exception, that's why you can not catch it.

check the following script:

<?php

echo "<pre>";
try{
    new SoapClient('a',['cache_wsdl' =>  WSDL_CACHE_MEMORY]);
    echo "client created\n";
}catch(SoapFault $e){
    echo "exception thrown\n";
}

echo "php error:\n";

print_r( error_get_last() );

output:

exception thrown
php error:
Array
(
    [type] => 1
    [message] => SOAP-ERROR: Parsing WSDL: Couldn't load from 'a' : failed to load external entity "a"

    [file] => /home/cebe/dev/yiisoft/yii2/apps/basic/web/soap.php
    [line] => 5
)

this seems to be a bug in the SoapClient implementation. it throws exception AND sets the fatal error in PHP.

in PHP 7 you could work around this issue by calling error_clear_last() in the catch clause but that function does not exist in php 5.x

Should be reported to PHP it seems, right?

yep.

Seem to be already reported: https://bugs.php.net/bug.php?id=70469 @cebe, @abolfazl-ziaratban please vote on it.

Since we can't affect this behavior from within Yii, I'm closing the issue.

also a duplicate of #9650

In version php 5.4.16 in Yii2 i have solved so
try {
$this->soapClient = new SoapClient($wsdl, $options);
//без exit не сработает, в данной версии php невозможно решить малой кровью
} catch (Exception $e) {
exit;
}

try {
            $short_link = new \SoapClient('http://test.com/test/ws/Landing/?wsdl', $options);
        }catch (\SoapFault $e){
            var_dump($e);
            throw new \yii\web\HttpException(500);
        }

and still cant to handle with SOAP-ERROR: Parsing WSDL: Couldn't load from , any ideas?

Soap suck...

Simple solve problem:

try {
   $wsdl = 'http://test.com/test/ws/Landing/?wsdl';
   simplexml_load_file($wsdl);
   $short_link = new \SoapClient($wsdl, $options);
} catch (\Exception $e) {
   var_dump($e);
}

If $wsdl is empty or not available simplexml_load_file throw an ErrorException

Was this page helpful?
0 / 5 - 0 ratings