waspinator waspinator - 2 years ago 333
PHP Question

Handle SOAP fatal error when there is a connection issue

I'm trying to connect to a soap service using a WSDL file in php 5.6

The snippet below works fine if I'm on the network, but if I'm disconnected I get a fatal error.

try {
$soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
catch (SoapFault $fault) {
echo 'poop';
catch (Exception $exception) {
echo 'pee';

edit: it does seem to do something with the SoapFault, because I can see my 'poop' debug message, but it still results in a fatal error

These are the errors I get

Warning (2): SoapClient(): php_network_get_addresses: getaddrinfo failed: No such host is known.
Warning (2): SoapClient(http://soap.service.com/serivce.svc) [soapclient.soapclient]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known.
Error: SOAP-ERROR: Parsing Schema: can't import schema from 'http://soap.service.com/serivce.svc'

How can I gracefully handle the error so that php continues to run, so I can set a variable and render an HTML page indicating that there was a problem connecting

Answer Source

This was a cakephp issue


$restore = error_reporting(0);
try {
    $soap_client = new SoapClient($wsdl_file, ['exceptions' => true]);
catch (SoapFault $e) {
    trigger_error($e->getMessage()); // Overwrites E_ERROR with E_USER_NOTICE
finally {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download