Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - fatal error , try-catch not working

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.

like image 466
Think Big Avatar asked Jan 31 '26 13:01

Think Big


1 Answers

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);
like image 155
vadgus Avatar answered Feb 03 '26 09:02

vadgus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!