[ACCEPTED]-SoapFault exception: [HTTP] Error Fetching http headers-soap

Accepted answer
Score: 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 25 will always try to use a persistent connection 24 to the web service by sending the Connection: Keep-Alive HTTP 23 header. If the server always closes the 22 connection and has not done so (PHP has 21 not received EOF), you can get Error fetching http headers when PHP tries 20 to reuse the connection that is already 19 closed on the server side.

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

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

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

A third solution is to just close 3 the connection just after the response is 2 received. This can be done by setting SoapClients 1 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;
Score: 0

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

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

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

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

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

Don't forget to reload apache. service httpd reload

More Related questions