The Could Not Connect to Host error message when using PHP SoapClient can be a tricky sucker to trace the root of. There are varying responses across the internets to what may cause this problem such as firewalls and proxies.

But there is one other thing you can check. Look through the WSDL for the soap:address locations. You may find that a server name is given that the machine running your code does not have access to.

Take a look at the example below;

<service name="SomeService">
  <port binding="tns:WebServiceBinding" name="WebServicePort">
    <soap:address location="http://appserver3:8080/webservices/WebServiceEndPoint"/>
  </port>
</service>

I experienced the issue when deploying my code from my dev machine to a live server. I couldn’t understand why my dev machine was fine calling the web services and the live server was not.

That’s because I had forgotten that I had provided access to my dev machine in the early days of coding to the appserver3 server name by virtue of a hosts file entry. Looking in my hosts file revealed

123.123.123.123  appserver3

And this is what was missing on the live server.

So if you get this error and you just can’t figure it out, take a wander through the WSDL for location URLs that may not be accessible to the server running your code. If you have a server name rather than an IP as in the example above, then add a hosts file mapping to the IP and you’re away.