16

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?

Syed Tayyab Ali
  • 3,643
  • 7
  • 31
  • 36

3 Answers3

30

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;
    }
}
HNygard
  • 4,526
  • 6
  • 32
  • 40
  • 9
    Since PHP 5.4 this flag is now available: The keep_alive option is a boolean value defining whether to send the Connection: Keep-Alive header or Connection: close. (http://www.php.net/manual/en/soapclient.soapclient.php) – heuri May 20 '14 at 19:46
  • It seems a shame that it requires turning off the keepalive to avoid this; I may be misunderstanding things, but shouldn't the server response indicate how long the connection can be kept alive for and then the SoapClient should just know not to reuse it after it times out (and, ideally, reconnect transparently)? – El Yobo Nov 17 '15 at 01:06
  • "The server does not support Keep-Alive connections (which my answer covers).". I had a buggy server I was talking to. It did not handle the Keep-Alive connections correctly and I had to turn it off. – HNygard Nov 17 '15 at 09:55
0

I had the same issue and tried the following by disabling keep_alive.

$api_proxy = new SoapClient($api_url, array('trace' => true, 'keep_alive' => false));

However, that didn't work for me. What worked worked for me was disabling the SOAP cache. It appears to have been caching the wrong requests and after disabling I actually noticed my requests were executing faster.

On a linux server you can find this in your /etc/php.ini file.

Look for soap.wsdl_cache_enabled=1 and change it to soap.wsdl_cache_enabled=0.

Don't forget to reload apache. service httpd reload

SeniorDeveloper
  • 886
  • 1
  • 12
  • 22
  • This means the wsdl will be fetched for each request. Bad idea and a massive performance hit. – ibram Dec 10 '20 at 20:27
-12

One of my process within web service was taking long time to execute. Therefore I was getting soapfault exeception.

Syed Tayyab Ali
  • 3,643
  • 7
  • 31
  • 36