博主信息
博文 1
粉丝 0
评论 0
访问量 991
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
curl 获取 https 请求方法
炼金术师的博客
原创
993人浏览过

http://blog.csdn.net/fdipzone/article/details/39611461

今日在做一个项目,需要curl获取第三方的API,对方的API是https方式的。

之前使用curl能获取http请求,但今天获取https请求时,出现了以下的错误提示:证书验证失败。


[plain] view plain copy

SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed  


解决方法,在curl请求时,加入



[php] view plain copy

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在  


curl https请求代码



[php] view plain copy

<?php  

/** curl 获取 https 请求 

* @param String $url        请求的url 

* @param Array  $data       要發送的數據 

* @param Array  $header     请求时发送的header 

* @param int    $timeout    超时时间,默认30s 

*/  

function curl_https($url, $data=array(), $header=array(), $timeout=30){  

  

    $ch = curl_init();  

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在  

    curl_setopt($ch, CURLOPT_URL, $url);  

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  

    curl_setopt($ch, CURLOPT_POST, true);  

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));  

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   

    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  

  

    $response = curl_exec($ch);  

  

    if($error=curl_error($ch)){  

        die($error);  

    }  

  

    curl_close($ch);  

  

    return $response;  

  

}  

  

// 调用  

$url = 'https://www.example.com/api/message.php';  

$data = array('name'=>'fdipzone');  

$header = array();  

  

$response = curl_https($url, $data, $header, 5);  

  

echo $response;  

?>  


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学