场景:

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. Linux基础(01)开发环境的搭建

    内核源码下载 : https://blog.csdn.net/u011375704/article/details/81866427 1.在虚拟机安装Ubuntu 14.04版本 (安装时设置好用户名 ...

  2. IAR_STM32_CCM内存使用

    在IAR中,硬件环境为STM32F464单片机,额外的CCM不能用DMA访问,但可以作为内部额外扩充RAM使用 修改文件中的内容为: define symbol __ICFEDIT_region_RO ...

  3. Dart方法基础知识

    方法定义: void main(List args){ print(args); print(getPerson('wwk', 32)); } /*String getPerson(String na ...

  4. SocketChannel简述

    前言 在前面的Channel概述的分类中提到过SocketChannel主要是用来基于TCP通信的通道.这篇文章详细介绍下SocketChannel SocketChannel是什么 SocketCh ...

  5. SpringBoot中使用@scheduled定时执行任务需要注意的坑

    spring boot: 计划任务@ EnableScheduling和@Scheduled @Scheduled中的参数说明 @Scheduled(fixedRate=2000):上一次开始执行时间 ...

  6. RabbitMQ学习之Topics(5)

    上一节,我们讲了direct exchange,这节我们讲下topic exchange Topic exchange 发送到topic exchange的messages不可以有一个随意的routi ...

  7. Python中的单例设计

    01. 单例设计模式 设计模式 设计模式 是 前人工作的总结和提炼,通常,被人们广泛流传的设计模式都是针对 某一特定问题 的成熟的解决方案 使用 设计模式 是为了可重用代码.让代码更容易被他人理解.保 ...

  8. python基础04--list,cou,dict

    1.1 列表list 1.列表可以完成大多数集合类的数据结构实现.列表中元素的类型可以不相同,它支持数字,字符串,列表,元组,集合,字典 2.列表是有序的, 可以索引,切片 3.List中的元素是可以 ...

  9. 记一下python的method resolution order(MRO)机制

    一直用python都是拿着cookbook和库的文档直接撸,很少会把细节过得那么彻底,遇到问题才会翻文档. 今天看到这个例子的时候我突然触及了我的盲区,我不确定这样的继承层级调用super.foo() ...

  10. kubernetes V1.16 Ingress-nginx部署

    Ingress 在Kubernetes中,服务和Pod的IP地址仅可以在集群网络内部使用,对于集群外的应用是不可见的.为了使外部的应用能够访问集群内的服务,在Kubernetes中可以通过NodePo ...