场景:

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()函数。

PHP函数file_get_contents()使用 https 协议时报错:SSL operation failed的更多相关文章

  1. CentOS上svn checkout时报错SSL handshake failed: SSL error: Key usage violation in certificate has been det

    局域网安装了个SVN在checkout的时候报错 SSL handshake failed: SSL error: Key usage violation in certificate has bee ...

  2. 只要是使用函数file_get_contents访问 https 的网站都要开启

    使用file_get_contents();报错failed to open stream: Unable to find the socket transport "ssl" - ...

  3. svn执行clean up 操作时报错 "Previous operation has not finished; run 'cleanup' if it was interrupted"解决如下!

    今天在项目中更新的时候,突然间爆了一个svn的这个错误,当时提示我去clean up操作,结果我执行clean up操作时候,还是报错,后来坚持出来,是因为ios项目中的一个图标出了问题,使svn进入 ...

  4. 转 关于Https协议中的ssl加密解密流程

    关于Https协议中的ssl加密解密流程 2016年09月28日 09:51:15 阅读数:14809 转载自:http://www.cnblogs.com/P_Chou/archive/2010/1 ...

  5. (转)svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrupted”

    今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not finished; run 'cleanup' if it was interrup ...

  6. 使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"

    PHPMailer项目地址:https://github.com/PHPMailer/PHPMailer 项目中用到PHPMailer,使用过程中报错:"Connection failed. ...

  7. file_get_contents(): SSL operation failed with code 1

    出现file_get_contents(): SSL operation failed with code 1的错误 方法需要添加参数,如下: $stream_opts = [ "ssl&q ...

  8. Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...

  9. 在linux下的apache配置https协议,开启ssl连接

    环境:linux 配置https协议,需要2大步骤: 一.生成服务器证书 1.安装openssl软件 yum install -y openssl mod_ssl 2.生成服务器私匙,生成server ...

随机推荐

  1. HUT 排序训练赛 G - Clock

    Clock Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u [Submit]   [Go ...

  2. GIT 安装和使用

    目录 GIT 安装和使用 一.GIT 介绍 二.GIT 安装 三.GIT 使用 1. 配置 2. 创建版本库 3. 远程仓库 4. 分支管理 5.标签管理 6. 自定义 GIT 安装和使用 一.GIT ...

  3. 常用Java API之Ramdom--用代码模拟猜数小游戏

    常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...

  4. 封装:WPF中可以绑定的BindPassWord控件

    原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...

  5. 解决IE下打印iframe时,页面比例变小的问题

    $('#reportFrame')[0].contentWindow.printMyself();//主页面调用iframe里的打印方法 //reportFrame里的方法 function prin ...

  6. 关闭Postman 证书的验证

    1.问题背景 使用自己生成的SSL证书,用Postman访问失败.需要忽略SSL证书的验证 2.关闭Postman 证书的验证 在Settings-General中 关闭SSL certificate ...

  7. 理解 BLS 签名算法

    理解 BLS 签名算法 来源 https://medium.com/cryptoadvance/bls-signatures-better-than-schnorr-5a7fe30ea716 原文标题 ...

  8. only size-1 arrays can be converted to Python scalars

    python版本:3.6.5 opencv版本:3.2.0 使用的jupyter notebook 源码如下: import cv2 import numpy as np import matplot ...

  9. intent 参数的规范

    对于采用 intent 参数的 Activity Manager 命令,您可以使用以下选项指定 intent: -a action 指定 intent 操作,如“android.intent.acti ...

  10. Go的基础类型

    1. 命名 Go语言中的函数名.变量名.常量名.类型名.语句标号和包名等所有的命名,都遵循一个简单的命名规则:一个名字必须以一个字母(Unicode字母)或下划线开头,后面可以跟任意数量的字母.数字或 ...