您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php使用curl获取https请求的方法_PHP

2025/11/4 7:24:01发布11次查看
本文实例讲述了php使用curl获取https请求的方法。分享给大家供大家参考。具体分析如下:
今日在做一个项目,需要curl获取第三方的api,对方的api是https方式的。
之前使用curl能获取http请求,但今天获取https请求时,出现了以下的错误提示:证书验证失败。
代码如下:
curl_setopt($ch, curlopt_ssl_verifypeer, false); // 跳过证书检查 
curl_setopt($ch, curlopt_ssl_verifyhost, true);  // 从证书中检查ssl加密算法是否存在
curl https请求代码
代码如下:
/** 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; 
?>
希望本文所述对大家的php程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product