curl如何获取https协议的状态码
$curl = curl_init();$url='https://';curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_header, 1); curl_setopt($curl,curlopt_nobody,true); curl_setopt($curl, curlopt_returntransfer, 1); $data = curl_exec($curl); echo curl_getinfo($curl,curlinfo_http_code); curl_close($curl);
我用这种方法打印出http的状态码没问题,但是换成https协议一直输出0. 请问https应该怎么获取
------解决思路----------------------
https 请求時要加上
curl_setopt($ch, curlopt_ssl_verifypeer, false); // 跳过证书检查
curl_setopt($ch, curlopt_ssl_verifyhost, false); // 从证书中检查ssl加密算法是否存在
<?php/** 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, false); // 从证书中检查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); $code = curl_getinfo($ch, curlinfo_http_code); if($error=curl_error($ch)){ die($error); } curl_close($ch); return array($code,$response);}// 调用$url = 'https://example.com/message.php';$data = array('name'=>'fdipzone');$header = array();list($code, $response) = curl_https($url, $data, $header, 5);echo $code;echo $response;?>
