Wednesday, July 16, 2014

PHP SoapClient error: Could not connect to host


It is a mystery why this piece of code caused a SoapFault exception:
 
<?php
 
  $wsdl = "http://myws.toptip.ca/myws.wsdl";

  $client = new SoapClient($wsdl, array(
                   "trace" => 1,
                   "exceptions" => 1));

  $parameters = array("Element1" => "data1", "Element2" => "data2");

  $response = $client->myFunction($parameters);

?>

An important fact was that the soap address location inside the WSDL is using a port other than 80, e.g.

 http://myws.toptip.ca:8080/mywebservice

The exception was captured in /var/log/httpd/error_log:

PHP Fatal error: Uncaught SoapFault exception: [Http] Could not connect to host in ...

I tried to use Wireshark to capture the packets sent and received. Surprisingly, there was no packet sent out to the server from SoapClient. Not even the attempt to connect.

Accidentally, I found out an interesting thing. When I changed the port number of the soap address location in the WSDL file to the default (80), e.g.

http://myws.toptip.ca/mywebservice

SoapClient actually tried to connect to the service.

Why???

Sorry, I don't have the answer. If you have a clue, please let me know.

However, I have a workaround.

I installed the squid proxy on port 80 of the localhost. Then when creating the SoapClient object, I added the "proxy_host" and "proxy_port" options. Bang! It worked!! SoapClient sent the request to the proxy and it was successfully forwarded to the web service location.

1 comment:

Anonymous said...

Literally happening to me right now. Do I really have to go through the process of creating a squid proxy, seems a bit out of hand.

Different API but same errors and symptoms

 
Get This <