PHP file_get_contents() 和设置请求头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2107759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP file_get_contents() and setting request headers
提问by Marcus
With PHP, is it possible to send HTTP headers with file_get_contents()?
使用 PHP,是否可以使用file_get_contents()?
I know you can send the user agent from your php.inifile. However, can you also send other information such as HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGE, and HTTP_CONNECTIONwith file_get_contents()?
我知道您可以从您的php.ini文件中发送用户代理。但是,你能不能也发送其他信息,如HTTP_ACCEPT,HTTP_ACCEPT_LANGUAGE和HTTP_CONNECTION用file_get_contents()?
Or is there another function that will accomplish this?
或者是否有其他功能可以实现这一点?
回答by Dominic Barnes
Actually, upon further reading on the file_get_contents()function:
实际上,在进一步阅读该file_get_contents()功能后:
// Create a stream
$opts = [
"http" => [
"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);
You may be able to follow this pattern to achieve what you are seeking to, I haven't personally tested this though. (and if it doesn't work, feel free to check out my other answer)
您可以按照这种模式来实现您的目标,不过我还没有亲自测试过。(如果它不起作用,请随时查看我的其他答案)
回答by Fabrice
Here is what worked for me (Dominic was just one line short).
这对我有用(多米尼克只有一行短)。
$url = "";
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
)
);
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context);
回答by ali_sed
You can use this variable to retrieve response headers after file_get_contents()function.
您可以使用此变量在file_get_contents()函数之后检索响应标头。
Code:
代码:
file_get_contents("http://example.com");
var_dump($http_response_header);
Output:
输出:
array(9) {
[0]=>
string(15) "HTTP/1.1 200 OK"
[1]=>
string(35) "Date: Sat, 12 Apr 2008 17:30:38 GMT"
[2]=>
string(29) "Server: Apache/2.2.3 (CentOS)"
[3]=>
string(44) "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT"
[4]=>
string(27) "ETag: "280100-1b6-80bfd280""
[5]=>
string(20) "Accept-Ranges: bytes"
[6]=>
string(19) "Content-Length: 438"
[7]=>
string(17) "Connection: close"
[8]=>
string(38) "Content-Type: text/html; charset=UTF-8"
}
回答by Laereom
Yes.
是的。
When calling file_get_contents on a url, one should use the stream_create_context function, which is fairly well documented on php.net
在 url 上调用 file_get_contents 时,应该使用 stream_create_context 函数,该函数在 php.net 上有很好的记录
This is more or less exactly covered on the following page at php.net in the user comments section: http://php.net/manual/en/function.stream-context-create.php
这在 php.net 的以下页面中的用户评论部分或多或少完全涵盖:http: //php.net/manual/en/function.stream-context-create.php
回答by phidah
Using the php cURL libraries will probably be the right way to go, as this library has more features than the simple file_get_contents(...).
使用 php cURL 库可能是正确的方法,因为这个库比简单的file_get_contents(...).
An example:
一个例子:
<?php
$ch = curl_init();
$headers = array('HTTP_ACCEPT: Something', 'HTTP_ACCEPT_LANGUAGE: fr, en, da, nl', 'HTTP_CONNECTION: Something');
curl_setopt($ch, CURLOPT_URL, "http://localhost"); # URL to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
$result = curl_exec( $ch ); # run!
curl_close($ch);
?>
回答by AlexB
回答by Dominic Barnes
Unfortunately, it doesn't look like file_get_contents()really offers that degree of control. The cURL extension is usually the first to come up, but I would highly recommend the PECL_HTTP extension (http://pecl.php.net/package/pecl_http) for very simple and straightforward HTTP requests. (it's much easier to work with than cURL)
不幸的是,它看起来并没有file_get_contents()真正提供这种程度的控制。cURL 扩展通常是第一个出现的,但我强烈推荐 PECL_HTTP 扩展(http://pecl.php.net/package/pecl_http)用于非常简单和直接的 HTTP 请求。(它比 cURL 更容易使用)

