PHP SoapClient:SoapFault 异常无法连接到主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30410658/
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 21:46:03  来源:igfitidea点击:

PHP SoapClient: SoapFault exception Could not connect to host

phpsoapsoap-clientsoapserver

提问by Simone Nigro

using this code I make soap requests

使用此代码我发出肥皂请求

$client = new SoapClient('http://example.com/soap/wsdl');

try {
    $result = $client->myMethod();
} catch (Exception $e) {
    echo $e->getMessage();
}

Sometimes (once out of ten), an exception is raised:

有时(十分之一),会引发异常:

SoapFault exception: [HTTP] Could not connect to host

SoapFault 异常:[HTTP] 无法连接到主机

My attempts

我的尝试

1) I looked for solutions, and I readthat this problem can be caused by wsdl cache, I disabled it in php.ini:

1)我寻找解决方案,我读到这个问题可能是由 wsdl 缓存引起的,我在php.ini

soap.wsdl_cache_enabled = 0
soap.wsdl_cache_ttl = 0

raises fewer exceptions

引发更少的异常

2) I tried to add to the host of windows (they are on windows) the resolution of the dns in windows/system32/drivers/etc/hosts:

2)我试图将 dns 的分辨率添加到 Windows 主机(它们在 Windows 上)windows/system32/drivers/etc/hosts

160.XX.XXX.XX example.com

raises fewer exceptions

引发更少的异常

3) I also tried to disable "Windows Firewall",

3)我也尝试禁用“Windows防火墙”,

raises fewer exceptions

引发更少的异常

4) I also tried to increase the default_socket_timeout in php.ini

4)我也尝试在php.ini中增加default_socket_timeout

default_socket_timeout = 90

nothing has changed

什么也没有变

The question

问题

The server soap does not seem to have problems.It is used without problems also from other sites. Is there anything else I can do?

服务器soap似乎没有问题。其他站点也可以正常使用它。还有什么我可以做的吗?

My settings

我的设置

PHP 5.6

Apache 2.4

Windows Server 2012

PHP 5.6

阿帕奇 2.4

视窗服务器 2012

UPDATE

更新

After many tests, I think that the problem is in the network, soap server is behind a reverse proxy, the problem appears in the proxy.

经过多次测试,我认为问题出在网络上,soap服务器后面有一个反向代理,问题出现在代理上。

采纳答案by akirk

You need to improve your debugging efforts even further:

您需要进一步改进调试工作:

ad 1) Does the WSDL change a lot? If not, turn onWSDL caching. You don't need to connect to the server to fetch the WSDL file.

广告 1) WSDL 变化很大吗?如果没有,请打开WSDL 缓存。您不需要连接到服务器来获取 WSDL 文件。

Is the output you mentioned really created in echo $e->getMessage();? You could add some debugging code inside the catch()block, for example:

你提到的输出真的是在 中创建的echo $e->getMessage();吗?您可以在catch()块内添加一些调试代码,例如:

  • check if you can connect to the server in another way (for example file_get_contents($soap_url))
  • if not, check what kind of error you are getting
    • print the time and check the error logs on the SOAP server
    • a 403 Forbiddenor similar error points on a problem on the server
    • a Could not connecterror points to a network problem (not exclusively but chances are higher for that)
  • You could try to put it in a loop (something along the lines of for ($i = 0; $i <= 5; $i++) { /* ... */ sleep(1); continue; }) to see if a second try would make it work
  • 检查您是否可以通过其他方式连接到服务器(例如file_get_contents($soap_url)
  • 如果没有,请检查您遇到的错误类型
    • 打印时间并检查 SOAP 服务器上的错误日志
    • 一个403个禁止在服务器上的问题或类似的错误点
    • a无法将错误点连接到网络问题(不仅如此,但可能性更高)
  • 您可以尝试将其放入一个循环中(类似于for ($i = 0; $i <= 5; $i++) { /* ... */ sleep(1); continue; }),以查看第二次尝试是否会使其起作用

All in all, "sometimes errors" are hard to debug, so either you should try to make it reproducible (which will point at the problem), or you need to record and output as much data as possible so that you can see where the problem was at the time when it occurred.

总而言之,“有时错误”很难调试,因此您应该尝试使其可重现(这将指向问题),或者您需要记录和输出尽可能多的数据,以便您可以看到在哪里问题出在它发生的时候。

回答by Daryl B

Im just going to add one more possibly useful tip to this tread. Its something you should check In addition to the other useful answers already written here.

我只是要为这个胎面添加一个更可能有用的提示。除了此处已写的其他有用答案之外,您还应该检查它的内容。

You might see this error if you upgrade from php 5.5 or older to PHP 5.6 or PHP7+. The default setting for soap in the newer versions of PHP is to verify the remote peer (and so it should be). If you are pulling a WSDL from HTTPS or have an HTTPS endpoint then it will check the CA. If it fails you will get this particular error.

如果您从 php 5.5 或更早版本升级到 PHP 5.6 或 PHP7+,您可能会看到此错误。在较新版本的 PHP 中,soap 的默认设置是验证远程对等点(应该如此)。如果您从 HTTPS 中提取 WSDL 或有一个 HTTPS 端点,那么它会检查 CA。如果失败,您将收到此特定错误。

There are two ways to solve this.

有两种方法可以解决这个问题。

1) Most secure option is to verify the peer. On a ubuntu system you would copy the certs to /usr/local/share/ca-certificates/and run update-ca-certificatesas root.

1) 最安全的选择是验证对等方。在 ubuntu 系统上,您可以将证书复制到/usr/local/share/ca-certificates/update-ca-certificates以 root身份运行。

2) Or you can disable the verification heres an example of how to do it. - This is generally not the preferred option, however it could provide a quick fix if the problem is urgent. Please don't be sloppy with your security though.

2)或者您可以在此处禁用验证,这是如何执行此操作的示例。- 这通常不是首选选项,但如果问题很紧急,它可以提供快速解决方案。不过请不要马虎。

$soapClient = new SoapClient($wsdl_url, array(
    //'trace' => 1,
    'stream_context' => stream_context_create(array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false, 
            'allow_self_signed' => true //can fiddle with this one.
        )
    ))
 ));

I find the PHP documentation is a bit fragmented when it comes to SOAP. And especially SOAP WSSE.

我发现 PHP 文档在涉及 SOAP 时有点零散。尤其是 SOAP WSSE。

回答by Borniet

Have you tried opening the URL in a browser or via wget or something alike? Then try refreshing a gazillion times, and see if any errors occur. My guess is that this is a network issue, but indeed, a wonky network makes it very hard to debug...

您是否尝试过在浏览器中或通过 wget 或类似方式打开 URL?然后尝试刷新无数次,看看是否发生任何错误。我的猜测是这是一个网络问题,但确实,不稳定的网络使得调试非常困难......

回答by Greg

Please check if keep alive is active on the server and try to change its parameters. It might be a problem with connection between WSDL fetching and request sending.

请检查服务器上的保持活动是否处于活动状态并尝试更改其参数。WSDL 获取和请求发送之间的连接可能存在问题。

If WSDL file never changes you can try to disable for and check if this error occurs again. Just set wsdl to null and set location and uri.

如果 WSDL 文件从未更改,您可以尝试禁用并检查此错误是否再次发生。只需将 wsdl 设置为 null 并设置位置和 uri。

回答by Mehdi Moshtaghi

Use this to clear soap cache in your script:

使用它来清除脚本中的soap缓存:

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