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

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

问题描述

我正在尝试使用 file_get_contents 从另一个文件中获取内容(不要问为什么).
我有两个文件:test1.phptest2.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的内容,并且正在被浏览器执行,从而获取cookies.

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

我正在检索内容:

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

但现在我得到了错误:

警告: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']."
"));
$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天全站免登陆