Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error fetching http headers in SoapClient

I'm trying to invoke a WS over https on a remote host:remote port and I get:

Error fetching http headers

using the PHP5 SoapClient; I can get the list of functions by doing $client->__getFunctions() but when I call $client->myFunction(...) I always get this error.

I've googled and found that increasing default_socket_timeout in php.ini should fix it, but it did not work.

Can anyone suggest me a solution?

EDIT: here is the code:

$wsdl="myWSDL";

$client = new SoapClient($wsdl,array('connection_timeout'=>5,'trace'=>true,'soap_version'=>SOAP_1_2));

var_dump($client->__getFunctions());

try {
    $response=$client->myFunction("1","2","3");
         } catch (SoapFault $fault) {
    var_dump($fault);
    }
}

always ends in the error.

How do I solve the problem?

like image 636
Cris Avatar asked Feb 22 '12 21:02

Cris


5 Answers

This error is often seen when the default_socket_timeout value is exceeded for the SOAP response. (See this link.)

Note from the SoapClient constructor: the connection_timeout option is used for defining a timeout value for connecting to the service, not for the timeout for its response.

You can increase it like so:

ini_set('default_socket_timeout', 600); // or whatever new value you want

This should tell you if the timeout is the issue, or whether you have a different problem. Bear in mind that you should not use this as a permanent solution, but rather to see if it gets rid of the error before moving on to investigate why the SOAP service is responding so slowly. If the service is consistently this slow, you may have to consider offline/batch processing.

like image 132
cmbuckley Avatar answered Nov 17 '22 08:11

cmbuckley


Just wanted to share the solution to this problem in my specific situation (I had identical symptoms). In my scenario it turned out to be that the ssl certificate provided by the web service was no longer trusted. It actually turned out to be due to a new firewall that the client had installed which was interfering with the SOAP request, but the end result was that the certificate was not being correctly served/trusted.

It was a bit difficult to track down because the SoapClient call (even with trace=1) doesn't give very helpful feedback.

I was able to prove the untrusted certificate by using:

openssl s_client -connect <web service host>:<port>

I know this won't be the answer to everyone's problem, but hopefully it helps someone. Either way I think it's important to realise that the cause of this error (faultcode: "HTTP" faultstring: "Error Fetching http headers") is usually going to be a network/socket/protocol/communication issue rather than simply "not allowing enough time for the request". I can't imagine expanding the default_socket_timeout value is going to resolve this problem very often, and even if it does, surely it would be better to resolve the issue of WHY it is so slow in the first place.

like image 14
Manachi Avatar answered Nov 17 '22 07:11

Manachi


I faced same problem and tried all the above solutions. Sadly nothing worked.

  1. Socket Timeout (Didn't work)
  2. User Agent (Didn't work)
  3. SoapClient configuration, cache_wsdl and Keep-Alive etc...

I solved my problem with adding the compression header property. This is actually required when you are expecting a response in gzip compressed format.

//set the Headers of Soap Client. 
$client = new SoapClient($wsdlUrl, array(
    'trace' => true, 
    'keep_alive' => true,
    'connection_timeout' => 5000,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'compression'   => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | SOAP_COMPRESSION_DEFLATE,
));

Hope it helps.

Good luck.

like image 9
Sanjay Mohnani Avatar answered Nov 17 '22 07:11

Sanjay Mohnani


I suppose it's too late, but I have the same problem. I try the socket timeout but it doesn't work. My problem was that the client and the server where in the same physical server. With the client code working in the same physical server , I get this error, but, with the same client code moved to my localhost, requesting the server, (client and server was executed in two differents mechines) all works fine.

Maybe that can help someone else!

like image 8
Jonathan Huet Avatar answered Nov 17 '22 07:11

Jonathan Huet


Setting 'keep_alive' to false worked for me:

new SoapClient($api_url, array('keep_alive' => false));
like image 7
Thiago Sathler Avatar answered Nov 17 '22 08:11

Thiago Sathler