无法使用file_get_contents发送Cookie [英] Not able to send cookies with file_get_contents

查看:208
本文介绍了无法使用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的内容,并被浏览器执行

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"))`;

我正在检索内容:

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

但现在我得到错误:


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

Warning: file_get_contents(http://www.domain.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 wroing here?

编辑:
忘记提及:没有streaming_context,页面只加载。

edit: forgot to mention: Without the streaming_context, the page just loads. But withouth 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.尝试从计算机获取URL 正在运行。这可能是因为您的网络服务器无法访问网站,本地计算机具有缓存副本或其他网络故障。

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的输出,重新创建完全相同的请求请求。

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.

最后,不 rel =nofollow noreferrer> 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天全站免登陆