PHP报错Warning:file_get_contents():SSL operation failed with code 1

PHP与SEO 思享 494浏览
摘要:
今天用在线界面验证百度真假蜘蛛,发现报道有误。错误提示显示SSL操作失败,代码为1。我在网上找到了解决方案,分享给大家参考。警告:file_get_contents():无法在/xxx/xxx.php的第31行启用加密。ip=220.181.108.86):无法打开流:在/XXX/Xxx.php online 31中操作失败。创建并返回一个资源流上下文,它包含在options中预先设置的所有参数的值。默认为空数组。

今天使用网上的接口验证百度真假蜘蛛,结果发现报错了。报错提示显示SSL operation failed with code 1.网上找到了解决办法,分享给大家参考学习

错误提示

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /xxx/xxx.php on line 31
Warning: file_get_contents(): Failed to enable crypto in /xxx/xxx.php on line 31
Warning: file_get_contents(https://api.uomg.com/api/ck_spider?ip=220.181.108.86): failed to open stream: operation failed in /xxx/xxx.php on line 31

解决方法

方法一

这个时候我们需要使用到了file_get_contents的第三个参数,然后使用stream_context_create函数生成对应的资源流,参数代码如下:

$stream_opts = [
            "ssl" => [
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ]
        ];
$result = file_get_contents("https://www.itbiancheng.com", false, stream_context_create($stream_opts));

方法二

直接在服务器上面安装证书根证书,命令如下:

yum install ca-certificates

服务器安装根证书

PS:如果代码中多处用到了file_get_contents,建议使用第二种方法

知识扩展

file_get_contents语法

file_get_contents(path,include_path,context,start,max_length)

file_get_contents相关参数

Warning:file_get_contents():SSL operation failed with code 1

stream_context_create相关

创建并返回一个资源流上下文,该资源流中包含了 options 中提前设定的所有参数的值。

 stream_context_create ([ array $options [, array $params ]] ) : resource

PS:options必须是一个二维关联数组,格式如下:$arr['wrapper']['option'] = $value 。默认是一个空数组

推荐阅读

PHP提示:Warning: count():Parameter must be an array or an object that implements Countable

错误原因:PHP7.2以后,count()函数的参数无效时会抛出warning警告。...

PHP7.4 报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated.

PHP7.4运行项目报错:Deprecated Functionality: implode(): Passing glue string after array is deprecated。只需交换 implode() 函数的两个参数!...

百度自动推送php实现代码

我们知道主动推送有助于搜索引擎抓取和收录网站内容,wordpress或者主流CMS其实已经集成了推送功能。但是如果我们要定期归还,而不是只在发布的时候推一次。这个功能可以通过结合PHP和Pagoda Panel的预定任务来实现。$api = '百度站......