curl 获取 https 请求方法2015-02-28今日在做一个项目,需要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;?>From:csdn博客 傲雪星枫