Set a cookie via CURL (php)

Solution:1

You could set cookie through the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options of curl in PHP. Example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFilePath);
$rsp = curl_exec($ch);
curl_close($ch);

Then the cookie content will be automatically saved in the $cookieFilePath, which is a local file path.

Solution:2

Add COOKIEFILE and COOKIEJAR as option and a text file named cookie.txt in your aplication directory

$cookie_file=realpath('cookie.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$rsp = curl_exec($ch);
curl_close($ch);

hope this will help