How to send cookies with file_get_contents

Solution 1:

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;

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.

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.

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

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.

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

Solution 2:

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.

One way to solve this problem is to use the session_write_close() to unlock the file and relock it after with 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.

But i'm sure this is not the best manner to manipulate session with an extend connection.

Btw: it's faster than cURL and fsockopen()

Let me know if you find something better.

Solution 3:

Just out of curiosity, are you attempting file_get_contents on 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_replace to replace ' ' with '%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.