3

When using file_get_contents() to request an external URL, how can I find out what headers I've sent, or alternatively what headers I am about to send? I'm basically looking for a request counterpart for $http_response_header or anything else I can use to extract the same data.

I know I get to set headers myself with stream_context_create(array ('http' => array ('header' => $header))), but I want to see what headers are actually being sent in the end, including default ones.

4

1 回答 1

1

如果您需要一个一致的标头来处理每个file_get_contents请求,请使用如下示例所示的流上下文:

<?php

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

?>

源: http: //php.net/manual/en/function.file-get-contents.php


如果您想查看要发送的标头,我能想到的最佳选择涉及curl(不是 file_get_contents),如下所示:

提出请求时;设置此选项:

curl_setopt($ch, CURLINFO_HEADER_OUT, true);

然后您可以调试请求并查看使用此发送的标头(在发送请求之后):

var_dump(curl_getinfo($ch));

更多信息:http ://www.php.net/manual/en/function.curl-getinfo.php

于 2013-11-05T14:43:46.223 回答