在 PHP SoapClient 中禁用证书验证
摘要:
有没有办法强制 PHP 中内置的 SoapClient 类通过 HTTPS 连接到具有无效证书的服务器?
我为什么要这样做?
我已经在还没有 DNS 条目或证书的服务器上部署了一个新应用程序。我想在设置 DNS 条目和修复证书之前尝试使用 SoapClient 连接到它,最合理的方法似乎是让客户端在测试期间忽略证书。
难道我没有意识到这是一个巨大的安全风险吗?
这仅用于测试。当服务投入生产时,将有一个有效的证书,并且客户端将被迫验证它。
Summary:
Is there a way to force the built in SoapClient-class in PHP to connect over HTTPS to a server with an invalid certificate?
Why would I want to do that?
I have deployed a new application on a server that has no DNS entry or certificate yet. I want to try connecting to it with a SoapClient before setting up the DNS entry and fixing the certificate, and the most reasonable way to do this seems to be to just make the client ignore the certificate during testing.
Don't I realise that this is a huge security risk?
This is only for testing. When the service goes into production, there will be a valid certificate in place, and the client will be forced to validate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这适用于 php 5.6.x;
或者如果你愿意,你可以添加到 cyrpto 方法
This is working on php 5.6.x;
or if you want you can add to cyrpto method
SoapClient
在其参数中采用一个流上下文,您可以自己创建该流上下文。这样您就可以控制传输层的几乎每个方面:文档:
SoapClient
takes a stream context in its parameters, which you can create yourself. That way you can control almost every aspect of the transport layer:Documentation:
接受的答案有效,但仅在非 WSDL 模式下有效。如果您尝试在WSDL模式下使用它(即您传递一个WSDL文件url作为第一个参数),您将面临下载WSDL文件时流上下文被忽略的事实。因此,如果 WSDL 文件也位于证书损坏的服务器上,它将失败,很可能会抛出消息
无法加载外部实体
。更多信息请参阅此处和此处。正如建议的,最简单的方法是手动下载 WSDL 文件并将本地副本传递给 SoapClient。例如,您可以使用
file_get_contents
使用已接受答案中的相同流上下文来下载它。请注意,创建 SoapServer 时还必须执行此操作。
The accepted answer works but only in the non-WSDL mode. If you try to use this in the WSDL mode (i. e. you pass a WSDL file url as the first argument) you will face the fact that the stream context is ignored when downloading WSDL files. So if the WSDL file is also located on a server with broken certificate, it will fail, most likely throwing the message
failed to load external entity
. See more here and here.As suggested, the simplest way around is to download the WSDL file manually and pass the local copy to the SoapClient. You can download it for example with
file_get_contents
using the very same stream context from the accepted answer.Note that you will also have to do this when creating a SoapServer.
PHP 5.6.8 的正确列表是
The correct list for PHP 5.6.8 is