PHP 7.4 对https需要更兼容配置.

2019-08-19 17:28:31 浏览数 (1)

比如. 平时我们读取https链接时. 旧版本是可以运行的. 但php7.4会报以下警告.

代码语言:javascript复制
file_get_contents('htts://www.qq.com');
// Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

$ghea = get_headers('htts://www.qq.com',1);
// Warning: get_headers(): Failed to enable crypto

要解决以上问题, 我们需要对这些函数的默认的流上下文进行配置. 如下代码.

代码语言:javascript复制
    $contextOptions = [
        'ssl' => [
            'verify_host' => false,
            'verify_peer' => false,
            'verify_peer_name' => false
        ]
    ];
    $context = stream_context_create($contextOptions);
    file_get_contents($url, false, $context);
代码语言:javascript复制
    stream_context_set_default(
        [
        'ssl' => [
            'verify_host' => false,
            'verify_peer' => false,
            'verify_peer_name' => false
        ]
       ]
    );
    get_headers($url,1);

好了, 问题解决.

END

0 人点赞