php使用curl擷取https請求的方法_PHP教程

來源:互聯網
上載者:User

php使用curl擷取https請求的方法


這篇文章主要介紹了php使用curl擷取https請求的方法,涉及curl針對https請求的操作技巧,非常具有實用價值,需要的朋友可以參考下

本文執行個體講述了php使用curl擷取https請求的方法。分享給大家供大家參考。具體分析如下:

今日在做一個項目,需要curl擷取第三方的API,對方的API是https方式的。
之前使用curl能擷取http請求,但今天擷取https請求時,出現了以下的錯誤提示:認證驗證失敗。

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

解決方案為在curl請求時,加入:

代碼如下:

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程式設計有所協助。

http://www.bkjia.com/PHPjc/956981.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/956981.htmlTechArticlephp使用curl擷取https請求的方法 這篇文章主要介紹了php使用curl擷取https請求的方法,涉及curl針對https請求的操作技巧,非常具有實用價值,需要的...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.