2019独角兽企业重金招聘Python工程师标准>>>
直接上代码:
function get_header( $url , $host_ip = null){
$ch = curl_init(); //curl初始化
if(!is_null($host_ip)){//需要绑定ip
$urldata = parse_url($url);
//url有参数
if (!empty($urldata['query']))
$urldata['path'] .= "?".$urldata['query'];
//域名设置
$headers = array("Host: ".$urldata['host']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//需要绑定的ip
$url = $urldata['scheme']."://".$host_ip.$urldata['path'];
}
curl_setopt($ch, CURLOPT_URL, $url);//获取的地址
curl_setopt ($ch, CURLOPT_HEADER, 1);//获取头信息
curl_setopt($ch, CURLOPT_NOBODY,1);//body信息不获取
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
//var_dump($result);
curl_close ($ch);//关闭curl
return $result;
}