php SoapFault 异常:无法连接到主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4318870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:38:46  来源:igfitidea点击:

SoapFault exception: Could not connect to host

phpsoap

提问by ujava

Sometimes fail to call the web service.

有时无法调用 Web 服务。

This problem happens all the time.

这个问题一直在发生。

What could be the problem?

可能是什么问题呢?

Error:
    SoapFault exception: [HTTP] Could not connect to host in 
    0 [internal function]: SoapClient->__doRequest('<?xml version="...', http://.', '', 1, 0)

采纳答案by ujava

The problem was solved.The problem is the cache

问题解决了 是缓存的问题

ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);

回答by VolenD

I am adding my comment for completeness, as the solutions listed here did not help me. On PHP 5.6, SoapClient makes the first call to the specified WSDL URL in SoapClient::SoapClientand after connecting to it and receiving the result, it tries to connect to the WSDL specified in the result in:

为了完整性,我添加了我的评论,因为此处列出的解决方案对我没有帮助。在 PHP 5.6 上,SoapClient 第一次调用指定的 WSDL URL SoapClient::SoapClient,在连接到它并接收到结果之后,它尝试连接到结果中指定的 WSDL:

<soap:address location="http://"/>

And the call fails with error Could not connect to hostif the WSDL is different than the one you specified in SoapClient::SoapClientand is unreachable (my case was SoapUI using http://host.local/).

Could not connect to host如果 WSDL 与您在其中指定的不同SoapClient::SoapClient并且无法访问(我的情况是使用http://host.local/ 的SoapUI ),则调用将失败并显示错误。

The behaviour in PHP 5.4 is different and it always uses the WSDL in SoapClient::SoapClient.

PHP 5.4 中的行为有所不同,它始终使用SoapClient::SoapClient.

回答by Rob Olmos

The host is either down or very slow to respond. If it's slow to respond, you can try increasing the timeout via the connection_timeoutoption or via the default_socket_timeoutsetting and see if that reduces the failures.

主机已关闭或响应非常慢。如果响应缓慢,您可以尝试通过connection_timeout选项或default_socket_timeout设置增加超时时间,看看是否可以减少失败。

http://www.php.net/manual/en/soapclient.soapclient.php

http://www.php.net/manual/en/soapclient.soapclient.php

http://www.php.net/manual/en/filesystem.configuration.php#ini.default-socket-timeout

http://www.php.net/manual/en/filesystem.configuration.php#ini.default-socket-timeout

You can also include error handling as zanlok pointed out to retry a few times. If you have users actually waiting on these SOAP calls then you'll want to queue them up and process them in the background and notify the user when they're finished.

您还可以像 zanlok 指出的那样包含错误处理以重试几次。如果您确实有用户在等待这些 SOAP 调用,那么您需要将它们排队并在后台处理它们,并在它们完成时通知用户。

回答by curzduff

there is a soap config section in your php.ini file, which control the wsdl access cache, may be shown as:

php.ini 文件中有一个soap 配置部分,它控制wsdl 访问缓存,可能显示为:

[soap] 
; Enables or disables WSDL caching feature. 
soap.wsdl_cache_enabled=1 ; 
Sets the directory name where SOAP extension will put cache files. 
soap.wsdl_cache_dir="/tmp" 
; (time to live) Sets the number of second while cached file will be used ; instead of original one.  
soap.wsdl_cache_ttl=86400

if wsdl file cache is enabled, it may cause this problem when changing wsdl URI in php code. in this example, you can just delete file start with wsdl-under /tmpdirectory. or you just set soap.wsdl_cache_enabled=0;and soap.wsdl_cache_ttl=0;PHP will fetch the wsdl file every-time you visit the page.

如果启用了 wsdl 文件缓存,则在 php 代码中更改 wsdl URI 时可能会导致此问题。在这个例子中,你可以只删除目录wsdl-下的文件开头/tmp。或者您只需设置soap.wsdl_cache_enabled=0;soap.wsdl_cache_ttl=0;PHP 将在您每次访问该页面时获取 wsdl 文件。

回答by MediaFormat

A misconfiguredservice leaves the default namespace with tempuri.org

一个错误配置的服务留下了与默认命名空间tempuri.org

This means the connection to the wsdl will work, but the function call will fail.

这意味着与 wsdl 的连接将工作,但函数调用将失败。

Stacktrace:

SoapClient->__doRequest('http://example.com...', 'http://tempuri.org....', 2, 0)

堆栈跟踪:

SoapClient->__doRequest('http://example.com...', ' http://tempuri.org....', 2, 0)

To remediate this, you must explicitly set the location using __setLocation()

要修复此问题,您必须使用显式设置位置 __setLocation()

$this->soapClient = new \SoapClient(WS_URL);
$this->soapClient->__setLocation(WS_URL);

回答by Juan Martín Pagella

This work for me

这对我有用

$opts = array(
  'ssl' => array('verify_peer' => false, 'verify_peer_name' => false)
);

if (!isset($this->soap_client)) {
  $this->soap_client = new SoapClient($this->WSDL, array(
    'soap_version'   => $this->soap_version,
    'location'       => $this->URL,
    'trace'          => 1,
    'exceptions'     => 0,
    'stream_context' => stream_context_create($opts)
  ));

回答by MaxP

In our case, it was a Ciphers negotiation problem. We were getting this error randomly. We solved our problem by forcing a Cipher like this:

在我们的例子中,这是一个密码协商问题。我们随机收到此错误。我们通过强制这样的密码解决了我们的问题:

$soapClient = new SoapClient ('http://example.com/soap.asmx?wsdl',
    array (
        "stream_context" => stream_context_create (
            array (
                'ssl' => array (
                    'ciphers'=>'AES256-SHA'
                )
            )
        )
    )
);

Looks like PHP wasn't negotiating the same Ciphers at each service call.

看起来 PHP 在每次服务调用时都没有协商相同的密码。

回答by Julian Duque Mejía

In my case it worked after the connection to the wsdl, use the function __setLocation()to define the location again because the call fails with the error:

在我的情况下,它在连接到 wsdl 后工作,使用该函数__setLocation()再次定义位置,因为调用失败并出现错误:

Could not connect to the host

无法连接到主机

This happens if the WSDL is different to the one specified in SoapClient::SoapClient.

如果 WSDL 与 中指定的不同,就会发生这种情况SoapClient::SoapClient

回答by Travis Hansen

I hit this issue myself and after much digging I eventually found this bug for ubuntu:

我自己遇到了这个问题,经过多次挖掘,我最终发现了 ubuntu 的这个错误:

https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/965371

https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/965371

specifically

具体来说

https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/965371/comments/62

https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/965371/comments/62

openssl s_client -connect site.tld:443failed however openssl s_client -tls1 -connect site.tld:443gave success. In my particular case part of the output included New, TLSv1/SSLv3, Cipher is RC4-MD5so I set the php context ssl/cipher value appropriately.

openssl s_client -connect site.tld:443失败却openssl s_client -tls1 -connect site.tld:443成功了。在我的特殊情况下,输出的一部分包括在内, New, TLSv1/SSLv3, Cipher is RC4-MD5因此我适当地设置了 php 上下文 ssl/cipher 值。

回答by Jacob

It seems the error SoapFault exception: Could not connect to hostcan be caused be several different things. In my cased it wasn't caused by proxy, firewall or DNS (I actually had a SOAP connection from the same machine working using nusoapwithout any special setup).

似乎错误SoapFault exception: Could not connect to host可能是由几种不同的原因引起的。在我的情况下,它不是由代理、防火墙或 DNS 引起的(我实际上有来自同一台机器的 SOAP 连接,使用nusoap工作,没有任何特殊设置)。

Finally I found that it was caused by an invalid pemfile which I referenced in the local_certoption in my SoapClient contructor.

最后我发现它是由pemlocal_cert在 SoapClient 构造函数的选项中引用的无效文件引起的。

Solution:When I removed the certificate chain from the pemfile, so it onlycontained certificate and private key, the SOAP calls started going through.

解决方案:当我从pem文件中删除证书链时,它只包含证书和私钥,SOAP 调用开始通过。