Reputation: 17049
There are 3 files in one folder:
curl.php:
$ch = curl_init('http://localhost/url.php');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_exec($ch);
url.php:
setcookie('test', 'foo', time()+60*60*24, '/');
cookies.txt: (empty)
When I run curl.php
, I expect that cookie will be saved in cookies.txt
.
But nothing happens.
What is wrong with this, why it does not working?
Upvotes: 1
Views: 6551
Reputation: 58004
Does it have permission to write the file in that directory with the user that it executes as?
If no cookies have been received, there will be no file saved and you should note that the cookies are saved to the file when the handle is closed.
It might be necessary to give the absolute path of the text file where the cookies should be saved to be sure you know where they end up.
Upvotes: 2
Reputation: 2017
Just define path.
Change curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
to curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
and should work.
And remember. COOKIEJAR is to save cookies, and COOKIEFILE is to load again to work with them.
Happy cURL ;P
Upvotes: 2
Reputation: 5616
According to PHP documentation:
CURLOPT_COOKIEJAR - The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
So you need to call curl_close($ch)
.
Upvotes: 2