php 无法使用 file_get_contents 发送 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/306591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not able to send cookies with file_get_contents
提问by Ikke
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.
我正在尝试使用 file_get_contents 从另一个文件中获取内容(不要问为什么)。我有两个文件:test1.php 和 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.
Test2.php 尝试获取 test1.php 的内容,并且正在被浏览器执行,从而获取 cookie。
To send the cookies with file_get_contents, I create a streaming context:
为了使用 file_get_contents 发送 cookie,我创建了一个流上下文:
$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"))`;
I'm retreiving the contents with:
我正在检索内容:
$contents = file_get_contents("http://www.domain.com/test1.php", false, $opts);
But now I get the error:
但现在我收到错误:
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
警告:file_get_contents( http://www.domain.com/test1.php) [function.file-get-contents]:无法打开流:HTTP 请求失败!未找到 HTTP/1.1 404
Does somebody knows what I'm doing wroing here?
有人知道我在这里做什么吗?
edit: forgot to mention: Without the streaming_context, the page just loads. But withouth the cookies I don't get the info I need.
编辑:忘了提及:没有streaming_context,页面就会加载。但是没有cookies我就得不到我需要的信息。
回答by Alan Storm
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
首先,这可能只是您问题中的一个错字,但 file_get_contents() 的第三个参数需要是您的流上下文,而不是选项数组。我用这样的东西进行了快速测试,一切都按预期进行
$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;
The error indicates the server is returning a 404. Try fetching the URL from the machine PHP is running onand notfrom 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.
该错误表明服务器返回 404。尝试从运行 PHP 的机器而不是从您的工作站/台式机/笔记本电脑获取 URL 。可能是您的 Web 服务器无法访问该站点、您的本地计算机有缓存副本或其他一些网络问题。
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.
确保在运行此测试时重复您的确切请求,包括您发送的 cookie(命令行 curl 对此很有用)。有问题的页面完全有可能在没有 cookie 的情况下在浏览器中正常加载,但是当您发送 cookie 时,站点实际上返回了 404。
Make sure that $_SERVER['HTTP_COOKIE'] has the raw cookie you think it does.
确保 $_SERVER['HTTP_COOKIE'] 具有您认为的原始 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, notjust the cookies.
如果您要进行屏幕抓取,请下载 Firefox 和 LiveHTTPHeaders 扩展的副本。执行所有必要的步骤以在 Firefox 中访问您想要的任何页面。然后,使用 LiveHTTPHeaders 的输出,重新创建完全相同的请求序列。包括每个标题,而不仅仅是 cookie。
Finally, PHP Curlexists for a reason. If at all possible, (I'm not asking!) use it instead. :)
最后,PHP Curl 的存在是有原因的。如果可能的话,(我不是问!)改用它。:)
回答by Yves Lange
Just to share this information.
When using session_start(), the session file is lock by PHP. Thus the actual script is the only script that can access the session file. If you try to access it via fsockopen()or file_get_contents()you can wait a long time since you try to open a file that has been locked.
只是为了分享这些信息。使用 时session_start(),会话文件被 PHP 锁定。因此,实际脚本是唯一可以访问会话文件的脚本。如果您尝试通过访问它,fsockopen()或者file_get_contents()您可以等待很长时间,因为您尝试打开已锁定的文件。
One way to solve this problem is to use the session_write_close()to unlock the file and relock it after with session_start().
解决此问题的一种方法是使用session_write_close()解锁文件,然后使用 重新锁定session_start()。
Example:
例子:
<?php
$opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
$context = stream_context_create($opts);
session_write_close(); // unlock the file
$contents = file_get_contents('http://120.0.0.1/controler.php?c=test_session', false, $context);
session_start(); // Lock the file
echo $contents;
?>
Since file_get_contents()is a blocking function, both script won't be in concurrency while trying to modify the session file.
由于file_get_contents()是阻塞函数,因此在尝试修改会话文件时,两个脚本都不会并发。
But i'm sure this is not the best manner to manipulate session with an extend connection.
但我确信这不是使用扩展连接操作会话的最佳方式。
Btw: it's faster than cURLand fsockopen()
顺便说一句:它比cURL和fsockopen()
Let me know if you find something better.
如果你找到更好的东西,请告诉我。
回答by Jason
Just out of curiosity, are you attempting file_get_contentson a page that has a space in it? I remember trying to use fgc on a URL that had a space in the name and while my web browser parsed it just fine, fgc didn't. I ended up having to use a str_replaceto replace ' ' with '%20'.
出于好奇,您是否正在尝试file_get_contents在其中有空格的页面上?我记得尝试在名称中有空格的 URL 上使用 fgc,虽然我的网络浏览器解析得很好,但 fgc 没有。我最终不得不使用 astr_replace将 ' ' 替换为 '%20'。
I would think that this should have been relatively easy to spot that though as it would report only half of the filename. Also, I noticed in one of these posts, someone used \r\n while defining the headers. Keep in mind that PHP doesn't like these to be in single quotes, but they work fine in double.
我认为这应该相对容易发现,尽管它只会报告文件名的一半。此外,我注意到在其中一篇文章中,有人在定义标题时使用了 \r\n。请记住,PHP 不喜欢将这些放在单引号中,但在双引号中它们可以正常工作。
回答by Sebastian Hoitz
Make sure that file1.php exists on the server. Try opening it in your own browser to make sure!
确保服务器上存在 file1.php。尝试在您自己的浏览器中打开它以确保!

