《PHP學習:php使用curl獲取https請求的方法》要點:
本文介紹了PHP學習:php使用curl獲取https請求的方法,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了php使用curl獲取https哀求的方法.分享給大家供大家參考.具體分析如下:
PHP應用今日在做一個項目,需要curl獲取第三方的API,對方的API是https方式的.
之前使用curl能獲取http哀求,但今天獲取https哀求時,出現了以下的錯誤提示:證書驗證失敗.
PHP應用SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed?
PHP應用解決方法為在curl哀求時,加入:
代碼如下:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查?
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);? // 從證書中檢查SSL加密算法是否存在
PHP應用curl https哀求代碼
代碼如下:
<?php?
/** curl 獲取 https 哀求
* @param String $url??????? 哀求的url
* @param Array? $data?????? 要l送的
* @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應用希望本文所述對大家的php程序設計有所幫助.
《PHP學習:php使用curl獲取https請求的方法》是否對您有啟發,歡迎查看更多與《PHP學習:php使用curl獲取https請求的方法》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/12267.html