欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php使用curl獲取https請(qǐng)求的方法

 更新時(shí)間:2015年02月11日 11:42:13   作者:傲雪星楓  
這篇文章主要介紹了php使用curl獲取https請(qǐng)求的方法,涉及curl針對(duì)https請(qǐng)求的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php使用curl獲取https請(qǐng)求的方法。分享給大家供大家參考。具體分析如下:

今日在做一個(gè)項(xiàng)目,需要curl獲取第三方的API,對(duì)方的API是https方式的。
之前使用curl能獲取http請(qǐng)求,但今天獲取https請(qǐng)求時(shí),出現(xiàn)了以下的錯(cuò)誤提示:證書驗(yàn)證失敗。

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

解決方法為在curl請(qǐng)求時(shí),加入:

復(fù)制代碼 代碼如下:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 從證書中檢查SSL加密算法是否存在

curl https請(qǐng)求代碼

復(fù)制代碼 代碼如下:
<?php 
/** curl 獲取 https 請(qǐng)求
* @param String $url        請(qǐng)求的url
* @param Array  $data       要發(fā)送的數(shù)據(jù)
* @param Array  $header     請(qǐng)求時(shí)發(fā)送的header
* @param int    $timeout    超時(shí)時(shí)間,默認(rèn)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; 
 

 
// 調(diào)用 
$url = 'https://www.example.com/api/message.php'; 
$data = array('name'=>'fdipzone'); 
$header = array(); 
 
$response = curl_https($url, $data, $header, 5); 
 
echo $response; 
?>

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論