mike
mike

Reputation: 1

Setting curl's cookie file manually

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.example.com    TRUE    /   FALSE   0   session 4029916%2C2010-04-30T22%3A51%3A52%2C1243925043100000000000000000000000000000

I'm getting cookies back from an API request, and I need to use those cookies for the following request, how can I set the cookies retrieved from the request to the cookie file that curl uses?

Above you can see an example, I need to write 3 cookies to this file with a different domain.

How can this be done?

Upvotes: 0

Views: 2631

Answers (1)

Mihai Iorga
Mihai Iorga

Reputation: 39704

You need to add 2 parameters to the cURL request

$cookie_file = "/directory/to/cookie1.txt";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

cookie1.txt has to writable by cURL. Leave the rest to cURL, it will know to put multiple domains and use them.

Upvotes: 2

Related Questions