curl 擷取 https 要求方法,curlhttps
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請求代碼
<?php/** curl 擷取 https 請求* @param String $url 請求的url* @param Array $header 請求時發送的header* @param int $timeout 逾時時間,預設30s*/function curl_https($url, $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, ''); 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';$header = array('content-type:application/json');$response = curl_https($url, $header, 5);echo $response;?>
curl https
在回調地址的頁面上用session或其它方式儲存url,在curl頁就可以調用了
php 用curl擷取https地址
你就算是
注釋1和注釋2都沒有他照樣會有資料輸出
因為 他預設是輸出你獲得到的資料
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//輸出內容為字串
你要加上上面這一句話
這樣就不會輸出擷取到的資料