在 PHP SoapClient 中禁用证书验证

发布于 2024-12-20 06:54:06 字数 312 浏览 0 评论 0原文

摘要:
有没有办法强制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初相遇 2024-12-27 06:54:07
"verify_peer"=>false,
"verify_peer_name"=>false,

这适用于 php 5.6.x;

$arrContextOptions=stream_context_create(array(
            "ssl" => array(
                 "verify_peer" => false,
                 "verify_peer_name" => false,
            )));
$this->client = new \SoapClient("https://tests.com?WSDL",
              array(
                //"soap_version" => SOAP_1_2,
                "trace"      => 1,      // enable trace to view what is happening
                "exceptions" => 0,      // disable exceptions
                "cache_wsdl" => 0,      // disable any caching on the wsdl, encase you alter the wsdl
                "stream_context" => $arrContextOptions
              ) 
                    
            );

或者如果你愿意,你可以添加到 cyrpto 方法

$arrContextOptions=stream_context_create(array(
            "ssl"=>array(
                 "verify_peer"=>false,
                 "verify_peer_name"=>false,
                 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
            ));
            
"verify_peer"=>false,
"verify_peer_name"=>false,

This is working on php 5.6.x;

$arrContextOptions=stream_context_create(array(
            "ssl" => array(
                 "verify_peer" => false,
                 "verify_peer_name" => false,
            )));
$this->client = new \SoapClient("https://tests.com?WSDL",
              array(
                //"soap_version" => SOAP_1_2,
                "trace"      => 1,      // enable trace to view what is happening
                "exceptions" => 0,      // disable exceptions
                "cache_wsdl" => 0,      // disable any caching on the wsdl, encase you alter the wsdl
                "stream_context" => $arrContextOptions
              ) 
                    
            );

or if you want you can add to cyrpto method

$arrContextOptions=stream_context_create(array(
            "ssl"=>array(
                 "verify_peer"=>false,
                 "verify_peer_name"=>false,
                 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
            ));
            
风筝有风,海豚有海 2024-12-27 06:54:06

SoapClient 在其参数中采用一个流上下文,您可以自己创建该流上下文。这样您就可以控制传输层的几乎每个方面:

$context = stream_context_create([
    'ssl' => [
        // set some SSL/TLS specific options
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    ]
]);

$client  = new SoapClient(null, [
    'location' => 'https://...',
    'uri' => '...', 
    'stream_context' => $context
]);

文档:

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:

$context = stream_context_create([
    'ssl' => [
        // set some SSL/TLS specific options
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    ]
]);

$client  = new SoapClient(null, [
    'location' => 'https://...',
    'uri' => '...', 
    'stream_context' => $context
]);

Documentation:

送你一个梦 2024-12-27 06:54:06

接受的答案有效,但仅在非 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.

仄言 2024-12-27 06:54:06

PHP 5.6.8 的正确列表是

'ssl' => array('verify_peer_name'=>false, 'allow_self_signed' => true),

The correct list for PHP 5.6.8 is

'ssl' => array('verify_peer_name'=>false, 'allow_self_signed' => true),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文