SoapFault exception: Could not connect to host

The problem was solved.The problem is the cache

ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);

A misconfigured service leaves the default namespace with tempuri.org

This means the connection to the wsdl will work, but the function call will fail.

Stacktrace:

SoapClient->__doRequest('http://example.com...', 'http://tempuri.org....', 2, 0)

To remediate this, you must explicitly set the location using __setLocation()

$this->soapClient = new \SoapClient(WS_URL);
$this->soapClient->__setLocation(WS_URL);

I am adding my comment for completeness, as the solutions listed here did not help me. On PHP 5.6, SoapClient makes the first call to the specified WSDL URL in SoapClient::SoapClient and after connecting to it and receiving the result, it tries to connect to the WSDL specified in the result in:

<soap:address location="http://"/>

And the call fails with error Could not connect to host if the WSDL is different than the one you specified in SoapClient::SoapClient and is unreachable (my case was SoapUI using http://host.local/).

The behaviour in PHP 5.4 is different and it always uses the WSDL in SoapClient::SoapClient.


The host is either down or very slow to respond. If it's slow to respond, you can try increasing the timeout via the connection_timeout option or via the default_socket_timeout setting and see if that reduces the failures.

http://www.php.net/manual/en/soapclient.soapclient.php

http://www.php.net/manual/en/filesystem.configuration.php#ini.default-socket-timeout

You can also include error handling as zanlok pointed out to retry a few times. If you have users actually waiting on these SOAP calls then you'll want to queue them up and process them in the background and notify the user when they're finished.

Tags:

Php

Soap