PHP SoapClient: SoapFault exception Could not connect to host
Asked Answered
C

5

14

using this code I make soap requests

$client = new SoapClient('http://example.com/soap/wsdl');

try {
    $result = $client->myMethod();
} catch (Exception $e) {
    echo $e->getMessage();
}

Sometimes (once out of ten), an exception is raised:

SoapFault exception: [HTTP] Could not connect to host

My attempts

1) I looked for solutions, and I read that this problem can be caused by wsdl cache, I disabled it in php.ini:

soap.wsdl_cache_enabled = 0
soap.wsdl_cache_ttl = 0

raises fewer exceptions

2) I tried to add to the host of windows (they are on windows) the resolution of the dns in windows/system32/drivers/etc/hosts:

160.XX.XXX.XX example.com

raises fewer exceptions

3) I also tried to disable "Windows Firewall",

raises fewer exceptions

4) I also tried to increase the default_socket_timeout in php.ini

default_socket_timeout = 90

nothing has changed

The question

The server soap does not seem to have problems.It is used without problems also from other sites. Is there anything else I can do?

My settings

PHP 5.6

Apache 2.4

Windows Server 2012

UPDATE

After many tests, I think that the problem is in the network, soap server is behind a reverse proxy, the problem appears in the proxy.

Coquille answered 23/5, 2015 at 8:38 Comment(5)
Soapfault exception is an Exception so has a method ->getMessage() that may tell you what the SoapFault is?Genie
@NicholasAlexander yes, I change my question to make it clearerCoquille
Maybe this will help #4319370Midge
Do you have an antivirus software? It appears to be a network issue, so you can put in the catch another SoapClient attempt and see if it fails as well. If you have another server in the same network, you can try making the same SOAP request from it and see what happens.Debose
It might be that the TTL hasn't passed. Check the note regarding the WSDL cache in the docs: php.net/manual/en/soapclient.soapclient.php. Basically changing the setting has no effect until the time has passed.Conchitaconchobar
R
7

You need to improve your debugging efforts even further:

ad 1) Does the WSDL change a lot? If not, turn on WSDL caching. You don't need to connect to the server to fetch the WSDL file.

Is the output you mentioned really created in echo $e->getMessage();? You could add some debugging code inside the catch() block, for example:

  • check if you can connect to the server in another way (for example file_get_contents($soap_url))
  • if not, check what kind of error you are getting
    • print the time and check the error logs on the SOAP server
    • a 403 Forbidden or similar error points on a problem on the server
    • a Could not connect error points to a network problem (not exclusively but chances are higher for that)
  • You could try to put it in a loop (something along the lines of for ($i = 0; $i <= 5; $i++) { /* ... */ sleep(1); continue; }) to see if a second try would make it work

All in all, "sometimes errors" are hard to debug, so either you should try to make it reproducible (which will point at the problem), or you need to record and output as much data as possible so that you can see where the problem was at the time when it occurred.

Rebarbative answered 29/5, 2015 at 11:7 Comment(1)
After many tests, I think that the problem is in the network, soap server is behind a reverse proxy, the problem appears in the proxy. thanks for your helpCoquille
L
19

Im just going to add one more possibly useful tip to this tread. Its something you should check In addition to the other useful answers already written here.

You might see this error if you upgrade from php 5.5 or older to PHP 5.6 or PHP7+. The default setting for soap in the newer versions of PHP is to verify the remote peer (and so it should be). If you are pulling a WSDL from HTTPS or have an HTTPS endpoint then it will check the CA. If it fails you will get this particular error.

There are two ways to solve this.

1) Most secure option is to verify the peer. On a ubuntu system you would copy the certs to /usr/local/share/ca-certificates/ and run update-ca-certificates as root.

2) Or you can disable the verification heres an example of how to do it. - This is generally not the preferred option, however it could provide a quick fix if the problem is urgent. Please don't be sloppy with your security though.

$soapClient = new SoapClient($wsdl_url, array(
    //'trace' => 1,
    'stream_context' => stream_context_create(array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false, 
            'allow_self_signed' => true //can fiddle with this one.
        )
    ))
 ));

I find the PHP documentation is a bit fragmented when it comes to SOAP. And especially SOAP WSSE.

Lieabed answered 8/6, 2016 at 23:12 Comment(1)
Same issue here, this solves the problem, I find it first in howtoforge.com/community/threads/… (Answer by "renky")Unamerican
R
7

You need to improve your debugging efforts even further:

ad 1) Does the WSDL change a lot? If not, turn on WSDL caching. You don't need to connect to the server to fetch the WSDL file.

Is the output you mentioned really created in echo $e->getMessage();? You could add some debugging code inside the catch() block, for example:

  • check if you can connect to the server in another way (for example file_get_contents($soap_url))
  • if not, check what kind of error you are getting
    • print the time and check the error logs on the SOAP server
    • a 403 Forbidden or similar error points on a problem on the server
    • a Could not connect error points to a network problem (not exclusively but chances are higher for that)
  • You could try to put it in a loop (something along the lines of for ($i = 0; $i <= 5; $i++) { /* ... */ sleep(1); continue; }) to see if a second try would make it work

All in all, "sometimes errors" are hard to debug, so either you should try to make it reproducible (which will point at the problem), or you need to record and output as much data as possible so that you can see where the problem was at the time when it occurred.

Rebarbative answered 29/5, 2015 at 11:7 Comment(1)
After many tests, I think that the problem is in the network, soap server is behind a reverse proxy, the problem appears in the proxy. thanks for your helpCoquille
H
1

Have you tried opening the URL in a browser or via wget or something alike? Then try refreshing a gazillion times, and see if any errors occur. My guess is that this is a network issue, but indeed, a wonky network makes it very hard to debug...

Holily answered 29/5, 2015 at 11:13 Comment(0)
W
1

Please check if keep alive is active on the server and try to change its parameters. It might be a problem with connection between WSDL fetching and request sending.

If WSDL file never changes you can try to disable for and check if this error occurs again. Just set wsdl to null and set location and uri.

Winnipegosis answered 30/5, 2015 at 19:11 Comment(0)
K
0

Use this to clear soap cache in your script:

ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
Knowledge answered 16/10, 2017 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.