使用 PHP,是否可以使用 file_get_contents()
发送 HTTP header ?
我知道您可以从您的 php.ini
文件发送用户代理。但是,您还可以使用 file_get_contents()
发送其他信息,例如 HTTP_ACCEPT
、HTTP_ACCEPT_LANGUAGE
和 HTTP_CONNECTION
吗?
或者是否有其他功能可以完成此操作?
最佳答案
实际上,在进一步阅读 file_get_contents()
函数后:
// Create a stream
$opts = [
"http" => [
"method" => "GET",
"header" => "Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
]
];
// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
// DOCS: https://www.php.net/manual/en/function.file-get-contents.php
$file = file_get_contents('http://www.example.com/', false, $context);
您也许可以遵循此模式来实现您的目标,但我还没有亲自测试过。 (如果它不起作用,请随时查看我的其他答案)
关于PHP file_get_contents() 和设置请求头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35878187/