CURL怎么获取HTTPS协议的状态码

PHP中文网
Release: 2019-10-17 11:16:34
Original
2594 people have browsed it

  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);
Copy after login


我用这种方法打印出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 = &#39;https://example.com/message.php&#39;;
$data = array(&#39;name&#39;=>&#39;fdipzone&#39;);
$header = array();

list($code, $response) = curl_https($url, $data, $header, 5);

echo $code;
echo $response;
?>
Copy after login

                        

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template