开发者

curl - get cookies into file

开发者 https://www.devze.com 2023-03-13 11:40 出处:网络
There are 3 files in one folder: curl.php: $ch = curl_init(\'http://localhost/url.php\'); curl_setopt($ch, CURLOPT_COOKIEFILE, \'cookies.txt\');

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 cooki开发者_JS百科e will be saved in cookies.txt.

But nothing happens.

What is wrong with this, why it does not working?


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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消