0

I am facing problem in handling "HTTP 500 Internal Server Error" in my PHP web application.
My web app is hitting multiple external web services in loop and if any one of them is getting above error, my script is terminated without executing other URLs.

Following is the code snippet I am trying to work on, any help will be appreciated.
Please let me know if anybody wants more information.

$arrURLs        = array('https://example1.com/abc1', 'https://example1.com/pqr2', 'https://example2.com/abc1', 'https://example2.com/pqr2');
$arrResponse    = array();
foreach( $arrURLs as $strURL ) {
    $soap_options = array('soap_version' => SOAP_1_1,
                            'debug'=>1,
                            'trace'=>1,
                            'exceptions'=>TRUE,
                            'cache_wsdl'=>WSDL_CACHE_NONE,
                            'connection_timeout'=> 500,
                            'stream_context' => stream_context_create(array('ssl' => array('verify_peer'=> false,'verify_peer_name'=>false,) ) ) );
    try{
        $client = new SoapClient( $strURL . '?wsdl', $soap_options );
        $client -> __setLocation( $strURL );
        $response = $client->method1();
        array_push( $arrResponse, $response );
    } catch(\Exception $e) {
        $strError = 'Exception while connecting to URL: ' . $strURL . 'Error Message: ' . $e->getMessage();
        echo $strError;
    }
}
// parse $arrResponse after getting from all web services;

Thanks.

tejas033
  • 175
  • 1
  • 1
  • 8
  • 3
    Why not catch the exception and retry the request? – Jim Wright Feb 17 '16 at 10:24
  • The "500" is caused by the script dying. You cannot continue the script after it has died. You need to keep it from dying in the first place. Check the error log for the particular reason of its demise. – deceze Feb 17 '16 at 10:31
  • Can I clear this up please. Is your server giving the 500 or is it the other servers that you are accessing that are giving the 500 error? – RiggsFolly Feb 17 '16 at 10:33
  • @JimWright: I am trying to catch it but the catch blow is not getting executed. RiggsFolly: other servers. deceze: Is their any way to prevent script from dying, because I want to execute script for all URLs in given array. – tejas033 Feb 17 '16 at 10:37
  • I know `SoapFault` extends `Exception` and therefore a `catch(\Exception $e)` should work, but does it make any difference if you change your catch to catch `SoapFault` – RiggsFolly Feb 17 '16 at 10:40
  • 1
    An error in the remote API should trigger an exception in the SoapClient. If you can't catch the exception are you sure the error isn't happening in your code? Do you have an example stack trace of any errors? – Jim Wright Feb 17 '16 at 10:42
  • Also it does not look like you are setting the option `'exceptions' => true` so its probablu not throwing exceptions – RiggsFolly Feb 17 '16 at 10:47
  • Actually I have had a thought, do you know which line of your code is recieving the error? – RiggsFolly Feb 17 '16 at 10:58
  • @RiggsFolly `SoapFault` worked, thnkx. I have `'exceptions' => true` already did this in above code. JimWright: Yes they have triggered error and now using `catch(\SoapFault $e)` my problem is solved. – tejas033 Feb 17 '16 at 11:12

1 Answers1

0
try{
    $client = new SoapClient( $strURL . '?wsdl', $soap_options );
    $client -> __setLocation( $strURL );
    $response = $client->method1();
    array_push( $arrResponse, $response );
} catch(\SoapFault $soapfault) {
    $strError = 'SoapFault exception is caught for URL: ' . $strURL . 'Error Message: ' . $soapfault->getMessage();
    echo $strError;
    continue;
} catch(\Exception $e) {
    $strError = 'Exception while connecting to URL: ' . $strURL . 'Error Message: ' . $e->getMessage();
    echo $strError;
    continue;
} 

In cakephp you need to explicitly handle SoapFault exceptions. You can use catch(SoapFault $soapfault) by putting use SoapFault; and catch(Exception $e) by putting use Cake\Core\Exception\Exception; before try catch block.

Make sure that $soap_options contains 'exceptions'=>TRUE, if exceptions are set to FALSE your exceptions will not get caught and script will terminate without catching your exception.

tejas033
  • 175
  • 1
  • 1
  • 8