dongtan2603 2010-11-27 09:55
浏览 148
已采纳

Php curl:问题设置HTTP_HOST

I have setup an internal proxy using php and curl. Most of it is done, however, I am having trouble setting HTTP_HOST header field. This is the code I am using:


Code on the proxy server::

$data_server_url = "http://IP_ADDRESS_OF_MY_CONTENT_SERVER/";
$request_uri="";
if(isset($_SERVER['REQUEST_URI'])) { $request_uri = $_SERVER['REQUEST_URI']; };
$curl_url="${data_server_url}${request_uri}";

//Pass all these fields as-they-are-got from the client to the content server.
$field_array=array("HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET",
      "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
      "HTTP_HOST", "HTTP_REFERER", "HTTP_USER_AGENT");

$curl_request_headers=array();

foreach ($field_array as &$field) {
   if(isset($_SERVER["$field"])) {
      $curl_request_headers["$field"]=$_SERVER["$field"];
   } else {
      $curl_request_headers["$field"]="";
   };
};

//Open connection
$curl_handle = curl_init();
//Set the url, number of POST vars, POST data
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_POST, count($_POST));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curl_request_headers);
//Execute post
$result = curl_exec($curl_handle);
//Close connection
curl_close($curl_handle);

However, on my content server, $_SERVER['HTTP_HOST'] is set to the its own IP address (it should be null or the HTTP_HOST variable through which the the proxy server is accessed).

Can anyone suggest what is the fix?

  • 写回答

3条回答 默认 最新

  • doucheng5705 2010-11-27 10:05
    关注

    From the documentation:

    value should be an array for the following values of the option parameter:

         ...
    
    CURLOPT_HTTPHEADER    An array of HTTP header fields to set, in the format
                           array('Content-type: text/plain', 'Content-length: 100')
    

    So yeah, I don't think you're setting them properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?