带有自签名证书的MAMP中的file_get_contents失败

问题描述 投票:5回答:1

我使用file_get_contents()在我的网站中包含svg文件。通常这适用于http,但我设置此站点以在本地使用https。当我这样做时,我收到此错误:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed on line 226

Warning: file_get_contents(): Failed to enable crypto on line 226

据我所知,这是因为证书是自签名的。我找到了一个本地解决方案,禁止对文件获取内容进行SSL检查。我使用cURL找到了类似的东西。然而,这似乎是一个安全漏洞。有没有办法在不禁用file_get_contents或cURL中的SSL检查的情况下完成这项工作?

php ssl svg file-get-contents self-signed-certificate
1个回答
2
投票

您需要允许使用自签名证书。为此,您需要定义自定义流上下文。

例:

file_get_contents(
    'https://self-signed.badssl.com/',
    false,
    stream_context_create([
        'ssl' => [
            'allow_self_signed'=> true
        ]
    ])
);
© www.soinside.com 2019 - 2024. All rights reserved.