php无法获得https网页内容的解决方案
发布于 作者:苏南大叔 来源:程序如此灵动~
程序员最经常使用的curl
,在php
下面也是经常用的到。但是,苏南大叔友情提示您:用curl
获取https
的网页内容的时候,也是需要用到证书的。而这么多年,估计大家从来都没有察觉过任何更新证书的动作,证明这可能是有自动renew
操作的。而目前苏南大叔手头的centos
系统,却存在有使用curl
无法获取https
内容的问题。
本文测试环境:centos
/php71
。
症状描述
利用curl
或者file_get_contents
获取远程http
的内容时,成功。但是获取https
的内容时,失败。错误提示文字类似于:
SSL operation failed with code 1. OpenSSL Error messages: error:14090086: SSL routines:ssl3_get_server_certificate:certificate vertify failed
开启openssl
保证php.ini
里面的openssl
扩展肯定要打开。这个操作比较基础,不做赘述。
extension=openssl.so
检测证书位置
查看一下openssl
证书的默认位置,检测openssl
的证书位置的代码:
php -r "print_r(openssl_get_cert_locations());"
default_cert_file
是默认位置。
当然,如果你不喜欢这个默认位置的话,也可以自定义证书位置。
修改方法为:php.ini
中的openssl.cafile
:
[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile='/code/cacert.pem'
查找php.ini
的方法为:
下载更新证书
证书的下载网址是:
您也可以下载以往的perm
证书文件:
选择一个最新的perm
证书下载即可。下载好的证书要放置在上述默认位置或者自定义好的位置。重启php
后,就应该可以解决问题了。
软链接特殊情况
在centos
下面,不过ll
一下这个证书,可以发现是个软链接。所以,你真正的放置证书的位置其实是有变化的。
结论
PS一下:这个curl的官网的域名,真心耐人寻味啊...
更多有关https
的相关话题,请点击查看:


