Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SoapFault exception: [HTTP] Error Fetching http headers

I am getting following exception in my php page.

SoapFault exception: [HTTP] Error Fetching http headers

I read couple of article and found that default_socket_timeout needs configuration. so I set it as follow. default_socket_timeout = 480

I am still getting same error. Can someone help me out?

like image 705
Syed Tayyab Ali Avatar asked Jan 28 '11 04:01

Syed Tayyab Ali


1 Answers

I have been getting Error fetching http headers for two reasons:

  1. The server takes to long time to answer.
  2. The server does not support Keep-Alive connections (which my answer covers).

PHP will always try to use a persistent connection to the web service by sending the Connection: Keep-Alive HTTP header. If the server always closes the connection and has not done so (PHP has not received EOF), you can get Error fetching http headers when PHP tries to reuse the connection that is already closed on the server side.

Note: this scenario will only happen if the same SoapClient object sends more than one request and with a high frequency. Sending Connection: close HTTP header along with the first request would have fixed this.

In PHP version 5.3.5 (currently delivered with Ubuntu) setting the HTTP header Connection: Close is not supported by SoapClient. One should be able to send in the HTTP header in a stream context (using the $option - key stream_context as argument to SoapClient), but SoapClient does not support changing the Connection header (Update: This bug was solved in PHP version 5.3.11).

An other solution is to implement your own __doRequest(). On the link provided, a guy uses Curl to send the request. This will make your PHP application dependent on Curl. The implementation is also missing functionality like saving request/response headers.

A third solution is to just close the connection just after the response is received. This can be done by setting SoapClients attribute httpsocket to NULL in __doRequest(), __call() or __soapCall(). Example with __call():

class MySoapClient extends SoapClient {
    function __call ($function_name , $arguments) {
        $response = parent::__call ($function_name , $arguments);
        $this->httpsocket = NULL;
        return $response;
    }
}
like image 199
HNygard Avatar answered Sep 17 '22 09:09

HNygard