PHP SOAP error catching

Active3 hr before
Viewed126 times

6 Answers


SOAP can fatal error calling the native php functions internally which prevents the SoapFaults being thrown so we need to log and suppress those native errors.,If you need to catch the SoapFault specifically, try the original answer which should allow you to suppress the fatal error that prevents the SoapFault being thrown,Unfortunately SOAP throws a fatal error when the service is down / unreachable rather than returning a SoapFault object., I haven't tried it but you probably need to use the original solution to get to the actual SoapFault returned. I recall the problem is that a native PHP function errors inside the SOAP logic that prevents the SoapFault from being thrown properly – MeatPopsicle Aug 28 '18 at 10:47

If you don't care about getting the SoapFault details and just want to catch any errors coming from the SoapClient you can catch "Throwable" in PHP 7+. The original problem was that SoapClient can "Fatal Error" before it throws a SoapFault so by catching both errors and exceptions with Throwable you will have very simple error handling e.g.

try {
   soap connection...
} catch (Throwable $e) {
   echo 'sorry... our service is down';

First you need to turn on exceptions handling:

try {
   $client = new SoapClient("", array(
      'exceptions' => true,
} catch (SoapFault $e) { // Do NOT try and catch "Exception" here
   echo 'sorry... our service is down';

AND THEN you also need to silently suppress any "PHP errors" that originate from SOAP using a custom error handler:


function handlePhpErrors($errno, $errmsg, $filename, $linenum, $vars) {
   if (stristr($errmsg, "SoapClient::SoapClient")) {
      error_log($errmsg); // silently log error
      return; // skip error handling
load more v

If exceptions is not set then SOAP call will throw an exception on error. is_soap_fault() checks if the given parameter is a SoapFault object. ,Example #2 SOAP's standard method for error reporting is exceptions,is_soap_fault — Checks if a SOAP call has failed,Example #1 is_soap_fault() example

load more v

I tried to call a SOAP API as a client and catch a fatal error, caused by missing WSDL file or network connectivity issues.,This is not an issue in CakePHP, but an annoying behavior in the PHP provided soap client. We have to do similar workarounds in the socket class and the related error swallower.,By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.,I just want to add I came across this problem and the suggested solution wasn't working for me what did work was catching \SoapFault with the backslash:

try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
catch (SoapFault $e) {
    echo 'a soap error occurred<br>';
echo 'I can still do stuff after the error<br>';
load more v

The problem of the SoapClient is that his temporary files are messed up somehow, maybe the PHP version was upgraded or maybe the /tmp dir was full on production and the server was not able to create new ones but yes, this is the problem.,So, the solution is simple, just delete all files from /tmp directory on the server that are starting with “wsdl-“ and the error will disappear.,Went to an error 500 in production server while using PHP SoapClient library to connect to some third-party API. The error was really strange, just error 500 and nothing more, even if you are using try catch, you won’t be able to catch and see the error. The Example:,This site uses Akismet to reduce spam. Learn how your comment data is processed.

Went to an error 500 in production server while using PHP SoapClient library to connect to some third-party API. The error was really strange, just error 500 and nothing more, even if you are using try catch, you won’t be able to catch and see the error.

The Example:

try {
   $client = new SoapClient("", ['exceptions' => true, 'trace' => true]);
} catch (SoapFault $fault) {
   trigger_error("SOAP Fault: (faultcode: ".$fault - > faultcode.
      ", faultstring: ".$fault - > faultstring.
      ")", E_USER_ERROR);
} catch (Exception $e) {
   trigger_error("SOAP Error: ".$e - > getMessage(), E_USER_ERROR);

Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. ,If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.,Understand that English isn't everyone's first language so be lenient of bad spelling and grammar., help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us

$skey = "FDGK:LKss#()#84$$$";
$productID = 5;
$data = "D359;00011,P,D359ZZ,SQKGLTKQKQYZHRA,ALNR,009350,20140228,005392;DEWALDH;D359;0";

try {
   $wsdl = "";
   $client = new SoapClient($wsdl);
   $result = $client - > __soapCall("InsertSerialAuthProduct", array("InsertSerialAuthProduct" => array("skey" => $skey, "ProductID" => $productID, "Data" => $data)));
} catch (SoapFault $E) {
   echo $E - > faultstring;
load more v

In JAX-WS, Java exceptions (java.lang.Exception) that are thrown by your Java Web service are mapped to a SOAP fault and returned to the client to communicate the reason for failure. SOAP faults can be one of the following types:,To use modeled faults, you need to create a custom Java exception and throw it from within your Web service.,Note that in addition to the custom exceptions that are thrown explicitly in your Web service and the SOAPFaultExceptions that are used to map exceptions that are not caught by your business logic, there are two other exceptions that might be communicated to the Web service client, and that you should be aware of.,The faults are returned to the sender only if request/response messaging is in use. If a Web service operation is configured as one-way, the SOAP fault is not returned to the sender, but stored for further processing.

Example 16-1 Example of SOAP 1.2 Fault Message

<?xml version="1.0"?>
<env:Envelope xmlns:env=>
            <env:Text xml:lang=en-US>Processing error<env:Text>
            <e:myFaultDetails xmlns:e=>
               <e:message>Name does not match card number</e:message>
load more v

Other "error-undefined" queries related to "PHP SOAP error catching"