PHP file_get_contents()和设置请求标头 [英] PHP file_get_contents() and setting request headers

查看:1996
本文介绍了PHP file_get_contents()和设置请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,是否可以发送带有 file_get_contents()的HTTP标头?



我知道你可以从 php.ini 文件发送用户代理。但是,您是否也可以发送其他信息,例如 HTTP_ACCEPT HTTP_ACCEPT_LANGUAGE HTTP_CONNECTION file_get_contents()



或者是否有另一个函数可以实现这个功能?实际上,在进一步阅读 file_get_contents()函数之后:

解决方案 p>

  //创建一个流
$ opts = [
http=> [
method=> GET,
header=> Accept-language:en \r \\\

Cookie:foo = bar \r\\\

]
];

$ context = stream_context_create($ opts);

//使用上面设置的HTTP标头打开文件
$ file = file_get_contents('http://www.example.com/',false,$ context);

您也许可以按照这种模式来实现您正在寻求的目标,我没有亲自虽然测试了这个。 (如果它不起作用,请随时查看我的其他答案)


With PHP, is it possible to send HTTP headers with file_get_contents() ?

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?

解决方案

Actually, upon further reading on the file_get_contents() function:

// 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)

这篇关于PHP file_get_contents()和设置请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆