PHP file_get_contents() and setting request headers(PHP file_get_contents() 和设置请求标头)
问题描述
使用 PHP,是否可以使用 file_get_contents()
发送 HTTP 标头?
With PHP, is it possible to send HTTP headers with file_get_contents()
?
我知道您可以从 php.ini
文件中发送用户代理.但是,您也可以使用 file_get_contents()
发送其他信息,例如 HTTP_ACCEPT
、HTTP_ACCEPT_LANGUAGE
和 HTTP_CONNECTION
吗?
I know you can send the user agent from your php.ini
file. However, can you also send other information such as HTTP_ACCEPT
, HTTP_ACCEPT_LANGUAGE
, and HTTP_CONNECTION
with file_get_contents()
?
或者是否有其他功能可以做到这一点?
Or is there another function that will accomplish this?
推荐答案
其实进一步阅读file_get_contents()
函数:
// Create a stream
$opts = [
"http" => [
"method" => "GET",
"header" => "Accept-language: en
" .
"Cookie: foo=bar
"
]
];
// 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);
您也许可以按照这种模式来实现您想要的,不过我还没有亲自测试过.(如果它不起作用,请随时查看我的其他答案)
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)
这篇关于PHP file_get_contents() 和设置请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP file_get_contents() 和设置请求标头
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01