当前位置:首页 > PHP教程 > PHP高级教程

php curl 获取https请求的2种方法

今天一个同事反映,使用curl发起https请求的时候报错:“ssl certificate problem, verify that the ca cert is ok. details: error:14090086:ssl routines:ssl3_get_server_certificate:certificate verify failed”

很明显,验证证书的时候出现了问题。

使用curl如果想发起的https请求正常的话有2种做法:

方法一、设定为不验证证书和host。

在执行curl_exec()之前。设置option

复制代码 代码如下:

$ch = curl_init();

......

curl_setopt($ch, curlopt_ssl_verifypeer, false);
curl_setopt($ch, curlopt_ssl_verifyhost, false);

方法二、设定一个正确的证书。

本地ssl判别证书太旧,导致链接报错ssl证书不正确。

我们需要下载新的ssl 本地判别文件

http://curl.haxx.se/ca/cacert.pem

放到 程序文件目录

curl 增加下面的配置

复制代码 代码如下:

   curl_setopt($ch,curlopt_ssl_verifypeer,true); ;
   curl_setopt($ch,curlopt_cainfo,dirname(__file__).'/cacert.pem');

大功告成

(本人验证未通过。。。报错信息为:ssl certificate problem, verify that the ca cert is ok. details: error:14090086:ssl routines:ssl3_get_server_certificate:certificate verify failed)

如果对此感兴趣的话可以参看国外一大神文章。http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!