3

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 – yii\base\ErrorException 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

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

why catch SoapFault not working ? but in single php script works and printed stars.

Think Big
  • 1,021
  • 14
  • 24
  • try catch won't catch a fatal. What error are you getting? (NOTE, unless it's a catchable fatal, but you don't say.) – Jonnix Jun 17 '16 at 16:07
  • seems it's a catchable fatal because try-catch in single php script work but in yii2 framework not working. what is difference between catchable and no catchable ? – Think Big Jun 17 '16 at 17:49
  • If I remember, catchable mean fatals are ones you can catch with a custom error handler, non catchable just kill the process then and there no matter what you do. – Jonnix Jun 17 '16 at 18:01
  • yes this is catchabl. why in yii not working ? – Think Big Jun 17 '16 at 18:19
  • Dunno, you _still_ haven't shown us the actual error. – Jonnix Jun 17 '16 at 18:20
  • error is not important, you run above code and you can see error.my goals is run catch block code in yii framework. Actually above code is correct in own php but in yii framework NO ! – Think Big Jun 17 '16 at 18:26
  • If you don't tell us the error, we can't help. Simple as. – Jonnix Jun 17 '16 at 18:28
  • post edited and i added error and example in two environment.(only php, yii framework) – Think Big Jun 17 '16 at 18:33
  • Add an extra `catch` block after the first one, i.e. `}catch(SoapFault $e){ echo '***************'; } catch(\Exception $e) { echo '*****************'; }` That should catch it. It looks like yii uses it's own custom error handler and transformed that WSDL error. – Jonnix Jun 17 '16 at 18:34
  • not working !!!! (last result) – Think Big Jun 17 '16 at 18:35
  • I had the same error. Got it just a bug php https://bugs.php.net/bug.php?id=47584 – Vitaly Jun 17 '16 at 18:37
  • At the same time triggered a fatal error and soapfault. A similar error in laravel https://github.com/laravel/framework/issues/6618 – Vitaly Jun 17 '16 at 18:39
  • this is not a php bug. in php without yii framewrok wroked but this yii framework not working. my php version is 5.6.3. – Think Big Jun 17 '16 at 18:40
  • Maybe I'm wrong. If you decide this problem would be cool! – Vitaly Jun 17 '16 at 18:41
  • this is a bug. [https://github.com/yiisoft/yii2/issues/11772](https://github.com/yiisoft/yii2/issues/11772) – Think Big Jun 18 '16 at 14:02

2 Answers2

1

The solution that helped me while I was googling same problem:

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

source: https://github.com/yiisoft/yii2/issues/11772#issuecomment-413887276


If it's nesessary to perform authorization it's possible to use curl

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "{$login}:{$password}");
curl_setopt($ch, CURLOPT_SSLCERT, $cert_path);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: text/xml; charset=utf-8"]);
$result = curl_exec($ch);
curl_close($ch);
vadgus
  • 11
  • 3
-1

you should add

define('YII_ENABLE_ERROR_HANDLER', false);

it works for me

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
amh
  • 11
  • 4