Php curl:设置 HTTP_HOST 时出现问题

发布于 2024-10-05 05:39:36 字数 1484 浏览 0 评论 0原文

我已经使用 php 和curl 设置了内部代理。大部分工作已完成,但是,我在设置 HTTP_HOST 标头字段时遇到问题。这是我正在使用的代码:


代理服务器上的代码::

$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);

但是,在我的内容服务器上, $_SERVER['HTTP_HOST'] 设置为其自己的 IP 地址(它应该为 null 或 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-12 05:39:36

来自文档

value 应该是选项参数的以下值的数组:

<前><代码> ...

CURLOPT_HTTPHEADER 要设置的 HTTP 标头字段数组,格式为
array('内容类型:文本/纯文本','内容长度:100')

所以是的,我认为您没有正确设置它们。

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.

§普罗旺斯的薰衣草 2024-10-12 05:39:36

$_SERVER 数组不使用与原始标头相同的键。你可以尝试这样的事情:

$pass_headers = array(
    'Host' => 'HTTP_HOST',
    'Accept' => 'HTTP_ACCEPT',
    'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
    'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
    'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
    'Connection' => 'HTTP_CONNECTION',
    'Referer' => 'HTTP_REFERER',
    'User-Agent' => 'HTTP_USER_AGENT',
);

$curl_request_headers = array();
foreach($pass_headers as $header_key => $server_key) {
    $curl_request_headers[] = $header_key.': '.$_SERVER[$server_key];
}

The $_SERVER array does not use the same keys as the raw headers. You might try something like this:

$pass_headers = array(
    'Host' => 'HTTP_HOST',
    'Accept' => 'HTTP_ACCEPT',
    'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
    'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
    'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
    'Connection' => 'HTTP_CONNECTION',
    'Referer' => 'HTTP_REFERER',
    'User-Agent' => 'HTTP_USER_AGENT',
);

$curl_request_headers = array();
foreach($pass_headers as $header_key => $server_key) {
    $curl_request_headers[] = $header_key.': '.$_SERVER[$server_key];
}
壹場煙雨 2024-10-12 05:39:36

您为标头指定了 PHP 在 $_SERVER 数组中使用的名称,但这是您在实际 HTTP 标头中使用的名称。例如,HTTP_HOST 标头应作为“Host”发送。

我建议更改您的 $fieldarray 以从 PHP 名称映射到正确的 HTTP 标头名称,正如 Ignacio 在另一个答案中所说,请检查curl 文档以了解传递这些标头的方式。

You are giving the headers the names PHP uses in the $_SERVER array, but this is the name you would use in an actual HTTP header. For example, the HTTP_HOST header should be sent as 'Host'.

I suggest changing your $fieldarray to map from the PHP name to the correct HTTP header name, and as Ignacio says in another answer, check the curl documentation for the way you are passing those headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文