file_get_contents receive cookies
you should use cURL
for that purpose, cURL
implement a feature called the cookie jar which permit to save cookies in a file and reuse them for subsequent request(s).
Here come a quick code snipet how to do it:
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
note: has to be noted you should have the pecl extension (or compiled in PHP) installed or you won't have access to the cURL API.
Send cookie with file_get_contents
#3
Cookie: user=3345; pass=abcd
file_get_contents get Cookies
According to php.net:
$http_response_header will be created in the local scope.
Because of this $http_response_header
is available only in the scope of get()
.
You can pass it like $this->get_cookies($http_response_header);
or create a property to store it.
Cookies are not being sent with file_get_contents or CURL requests
The headers may need to be separated by \r\n
rather than just \n
. You can also use an array, and they'll be sent properly.
'header' => array("Content-Type: application/x-www-form-urlencoded",
"Cookie: session_hash=123456789")
How to send cookies with 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;
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. :)
Php file_get_contents not capturing certain group of cookies
Several items to incorporate to solve this.
1. moved to curl
2. remove content length (this caused redirect following to timeout everytime when set)
3. Make a second request and store the cookie and present all stored cookies.
Had to use all the options below
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_SSL_VERIFYPEER => false,
Related Topics
Simplest Way to Increment a Date in PHP
PHP File Listing Multiple File Extensions
Can't Send Email to Addresses at My Own Domain
Storing Form Data as a Session Variable
Create New Variables from Array Keys in PHP
Single Quotes or Double Quotes for Variable Concatenation
How to Schedule the Execution of a PHP Script on the Server Side
Caching Http Responses When They Are Dynamically Created by PHP
Convert All Types of Smart Quotes with PHP
PHP Sentence Boundaries Detection
PHP JSON Encode - Malformed Utf-8 Characters, Possibly Incorrectly Encoded
How to Put PHP Inside JavaScript
PHP Get Url of Redirect from Source Url
How to Logout from a Session in PHP
Passing a Variable from One PHP Include File to Another: Global VS. Not
How to Prevent Pdo from Interpreting a Question Mark as a Placeholder