Error fetching http headers in SoapClient

2019-01-17 14:16发布

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

Error fetching http headers

using 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 to increase default_socket_timeout in php.ini 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 in fault.

12条回答
看我几分像从前
2楼-- · 2019-01-17 14:44

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.

查看更多
Rolldiameter
3楼-- · 2019-01-17 14:47

I had this problem and I checked, and in my case, was the Firewall. The PHP not show the error correctly. To perform the request, the Firewall answered:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
...
<html
...
<h1>Direct Access IP is not allowed</h1>
...
</html>

The SoapClient expects the SOAP envelope but receives a HTML code. That's why PHP responds with: "Error Fetching Http Headers" because it can not understand what he received in response. To resolve the problem, contact your network administrator to verify if there is any Firewall, NAT or proxy getting in the way and then ask them to make the necessary arrangements.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-17 14:56

'keep_alive' worked for me:

new SoapClient($api_url, array('keep_alive' => false));
查看更多
Rolldiameter
5楼-- · 2019-01-17 14:57

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

查看更多
Melony?
6楼-- · 2019-01-17 14:59

None of the above techniques worked for me.

When I analyzed the request header from __getLastRequestHeaders, I saw the following:

POST /index.php/api/index/index/?SID=012345 HTTP/1.1 Host: www.XYZ.com

The API URL I had been using was different, like www.ABC.com. I changed the API URL to www.XYZ.com/index.php/api?wsdl, and then it worked.

Both URLs returned the same WSDL from the same server, but only one allowed login.

查看更多
Explosion°爆炸
7楼-- · 2019-01-17 15:00

The configuration that has worked for me was defining at my php script the following parameters:

ini_set('default_socket_timeout', 5000);
$client = new \SoapClient($url,array(
    'trace' =>true,
    'connection_timeout' => 5000,
    'cache_wsdl' => WSDL_CACHE_NONE,
    'keep_alive' => false,
));

Please, comment.

The most important parameter definition, as per my experience with this issue was

ini_set('default_socket_timeout', 5000);

During my tests I have defined the default_socket_timeout to 5 seconds and the error 'Error Fetching http headers' was raised instantaneously.

I hope it helps you!

查看更多
登录 后发表回答