curl 获取绑定ip的url链接的头信息

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

直接上代码:

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;

         }



转载于:https://my.oschina.net/u/858306/blog/220651

你可能感兴趣的:(curl 获取绑定ip的url链接的头信息)