使用PHP,是否可以发送带有file_get_contents()
个字符的HTTP头?
我知道你可以从php.ini
文件中发送用户代理.但是,您是否也可以发送其他信息,例如HTTP_ACCEPT
、HTTP_ACCEPT_LANGUAGE
和HTTP_CONNECTION
与file_get_contents()
?
还是有其他功能可以实现这一点?
使用PHP,是否可以发送带有file_get_contents()
个字符的HTTP头?
我知道你可以从php.ini
文件中发送用户代理.但是,您是否也可以发送其他信息,例如HTTP_ACCEPT
、HTTP_ACCEPT_LANGUAGE
和HTTP_CONNECTION
与file_get_contents()
?
还是有其他功能可以实现这一点?
实际上,进一步阅读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);
你也许可以遵循这个模式来实现你想要的,但我个人还没有测试过.(如果不起作用,请随时查看我的其他答案)