如何使用file_get_contents发送cookie [英] How to send cookies with file_get_contents

查看:124
本文介绍了如何使用file_get_contents发送cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 file_get_contents 从另一个文件获取内容(不要问为什么).
我有两个文件: test1.php test2.php . test1.php 返回一个基于登录用户的字符串.

I'm trying to get the contents from another file with file_get_contents (don't ask why).
I have two files: test1.php and test2.php. test1.php returns a string, bases on the user that is logged in.

test2.php 尝试获取 test1.php 的内容,并由浏览器执行,从而获取cookie.

test2.php tries to get the contents of test1.php and is being executed by the browser, thus getting the cookies.

要发送带有 file_get_contents 的cookie,我创建一个流上下文:

To send the cookies with file_get_contents, I create a streaming context:

$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"))`;

我正在使用以下内容检索内容:

I'm retrieving the contents with:

$contents = file_get_contents("http://www.example.com/test1.php", false, $opts);

但是现在我得到了错误:

But now I get the error:

警告:file_get_contents( http://www.example.com/test1.php )[function.file-get-contents]:无法打开流:HTTP请求失败!找不到HTTP/1.1 404

Warning: file_get_contents(http://www.example.com/test1.php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

有人知道我在做什么错吗?

Does somebody knows what I'm doing wrong here?


忘了提及:如果没有 streaming_context ,则页面只会加载.但是,如果没有Cookie,我将无法获得所需的信息.

edit:
forgot to mention: Without the streaming_context, the page just loads. But without the cookies I don't get the info I need.

推荐答案

首先,这可能只是您输入问题的错字,但是file_get_contents()的第三个参数需要是您的流上下文,而不是选项数组.我用这样的东西进行了快速测试,一切都按预期运行

First, this is probably just a typo in your question, but the third arguments to file_get_contents() needs to be your streaming context, NOT the array of options. I ran a quick test with something like this, and everything worked as expected

$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
$context = stream_context_create($opts);
$contents = file_get_contents('http://example.com/test1.txt', false, $context);
echo $contents;

错误表明服务器正在返回404.请尝试从运行PHP的计算机上获取URL ,而不是从工作站/台式机/笔记本电脑获取不是.可能是您的Web服务器无法访问该站点,本地计算机具有缓存的副本或某些其他网络故障.

The error indicates the server is returning a 404. Try fetching the URL from the machine PHP is running on and not from your workstation/desktop/laptop. It may be that your web server is having trouble reaching the site, your local machine has a cached copy, or some other network screwiness.

请确保在运行此测试时重复您的确切请求,包括您要发送的Cookie(命令行curl对此非常有用).完全有可能该页面在没有cookie的浏览器中可以正常加载,但是当您发送cookie时,该网站实际上返回了404.

Be sure you repeat your exact request when running this test, including the cookie you're sending (command line curl is good for this). It's entirely possible that the page in question may load fine in a browser without the cookie, but when you send the cookie the site actually is returning a 404.

确保$ _SERVER ['HTTP_COOKIE']拥有您认为的原始cookie.

Make sure that $_SERVER['HTTP_COOKIE'] has the raw cookie you think it does.

如果要抓取屏幕,请下载Firefox和LiveHTTPHeaders扩展的副本.执行所有必要的步骤,以到达Firefox中所需的任何页面.然后,使用LiveHTTPHeaders的输出,重新创建完全相同的请求请求.包括每个标头,仅包含cookie.

If you're screen scraping, download Firefox and a copy of the LiveHTTPHeaders extension. Perform all the necessary steps to reach whatever page it is you want in Firefox. Then, using the output from LiveHTTPHeaders, recreate the exact same request requence. Include every header, not just the cookies.

最后,存在 PHP Curl 是有原因的.如果有可能,(我不问!)改用它.:)

Finally, PHP Curl exists for a reason. If at all possible, (I'm not asking!) use it instead. :)

这篇关于如何使用file_get_contents发送cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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