PHP函数file_get_contents()使用 https 协议时报错:SSL operation failed

场景:

file_get_contents() 函数是用于将文件的内容读入到一个字符串中,是读取文件内容常用的函数之一。

但是有时在服务器上使用file_get_contents() 函数请求https 协议的url文件时会报错误,无法正确读取文件内容,

查看log日志,日志内容类似如下:

PHP Warning:  file_get_contents(): Failed to enable crypto in ......
PHP Warning:  file_get_contents......: failedream: operation failed in ......
PHP Warning:  file_get_contents(): SSL operatwith code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verin ......
原因:

服务器未正确配置好https证书

解决方法:(三种解决方法)

方法一:

下载https证书到服务器

服务器 下载这个证书,http://curl.haxx.se/ca/cacert.pem
php.ini 配置
openssl.cafile = "/etc/ssl/certs/cacert.pem"//你实际下载证书的路径
重启 php 即可

 

方法二:

使用cURL 函数处理 https 的参数,获取文件内容

<?php
function getSSLPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSLVERSION,3); 
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

var_dump(getSSLPage("https://xxx.xxx.xxx"));
?>

引用:https://stackoverflow.com/questions/14078182/openssl-file-get-contents-failed-to-enable-crypto

 

方法三:

使file_get_contents()函数跳过https验证

$stream_opts = [
    "ssl" => [
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ]
]; 

$response = file_get_contents("https://xxx.xxx.xxx",false, stream_context_create($stream_opts));

 开发中建议使用cURL 函数替代file_get_contents()函数。

 

转载于:https://www.cnblogs.com/wenzheshen/p/11179533.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值