指定HOST访问特定网址

平时测试过程中,经常会需要编辑HOST文件来访问特定的服务器。实际上,这个过程也可以在代码中完成。这个实现方式的根本,就是在HTTP请求的Header中,指定请求的HOST。

1、使用CURL

$ curl --silent -H "Host:house.baidu.com" "60.28.244.21/xxx/xxx/x.php"

2、使用PHP的CURL函数指定

   //httpHeader   设置的 http head 参数 数组形式 如 array('Host: [client.51.com](http://client.51.com/)')
   function comm_curl_request($url,$postString='',$httpHeader='')
   {
      $ch = curl_init();
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$postString);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
      curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
      if(!empty($httpHeader) && is_array($httpHeader))
      {
         curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
      }
      $data = curl_exec($ch);
      $info = curl_getinfo($ch);
      //var_dump($info);
      curl_close($ch);
      return $data;
   }

3、使用file_get_contents函数

   <?php

   $opts = array('http' => array( 'header' => 'Host: house.baidu.com',)); 
   $context = stream_context_create($opts); 
   $result = file_get_contents('[http://60.28.244.21/a.php](http://10.6.6.6/a.php)', false, $context); 

   echo $result;
 ?> 

参考资料:
1、CURL请求指定HOST的URL
2、不用设置HOST,访问测试的HTTP接口

posted @ 2021-03-09 18:42  Cocowool  阅读(1560)  评论(0编辑  收藏  举报