SoapClient error fallback in PHP

15,859

This has already been discussed :

Rasmus himself proposed the following solution:

<?php  
try {  
    $x = @new SoapClient("non-existent.wsdl",array("exceptions" => 1));  
} catch (SoapFault $E) {  
    echo $E->faultstring; 
}  
echo "ok\n";
Share:
15,859
Yuval Adam
Author by

Yuval Adam

λf.(λx.f(x x)) (λx.f(x x))

Updated on July 19, 2022

Comments

  • Yuval Adam
    Yuval Adam almost 2 years

    In PHP, if you try to instantiate a new SoapClient, and the WSDL is not accessible (server down or whatever), a PHP fatal error is thrown:

    Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://example.com/servlet/app/SomeService?wsdl' : failed to load external entity "http://example.com/servlet/app/SomeService?wsdl"

    Fatal errors in PHP, as far as I know, are not recoverable.

    Is there any way to fallback from this? Can this fatal error somehow be avoided?


    Edit: I should say that I am running on PHP 5.2, if it makes any difference.

  • Patrick
    Patrick almost 11 years
    Unfortunately this solution does not seem to work in PHP 5.3.10.
  • Patrick
    Patrick almost 11 years
    I copied it verbatim into a fresh file and it is not getting to the echo "Ok"; part.
  • Patrick
    Patrick almost 11 years
    But, when I looked further down I did find the notes about disabling xdebug and that allowed the above to work.
  • JDuarteDJ
    JDuarteDJ almost 10 years
    best practice is try catch for sure.