Welcome to a tutorial on how to do PHP CURL calls with cookies. Need to do a server-to-server call that involves cookies? Well yes, CURL is fully capable of handling that with a few small tweaks.
To do a PHP CURL call with cookies, we use CURLOPT_COOKIEJAR
to specify where to save the cookie after the call ends, and CURLOPT_COOKIEFILE
to specify which cookie file to send to the remote server.
$cookie = "COOKIE.TXT";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "HTTP://SITE.COM");
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_exec($ch);
curl_close($ch);
That covers the basics, but read on for more examples!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
1) SET & GET COOKIE
1A) LOCAL SERVER CURL CALL
<?php
// (A) INIT CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/x-dummy.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// (B) SET COOKIE FILE
// CURLOPT_COOKIEFILE - SEND THIS COOKIE FILE TO THE REMOTE SERVER.
// CURLOPT_COOKIEJAR - SAVE COOKIE INTO THIS FILE AFTER CURL CLOSE.
$cookie = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt"; // NOTE: ABSOLUTE PATH
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else {
echo $result."\r\n\r\n";
$clist = curl_getinfo($ch, CURLINFO_COOKIELIST);
echo "LOCAL SERVER RECEIVED COOKIE - ";
print_r($clist);
}
curl_close($ch);
This is just a “slightly improved” version of the introduction snippet to output the exchange results between the local and remote servers.
1B) DUMMY REMOTE SERVER
<?php
// (A) OUTPUT COOKIE ON REMOTE SERVER
echo "REMOTE SERVER COOKIE - ";
print_r($_COOKIE);
// (B) SEND HEADER - UPDATE "TEST" COOKIE TIMESTAMP
setcookie("Test", date("Y-m-d H:i:s"));
On the dummy remote server – We simply output the received cookie and send a header to set the Test
cookie to the current timestamp.
1C) WHAT WILL HAPPEN
- On the first run –
- The remote server will not show any cookie values since it is a new connection.
- The remote server then responds with a set-cookie
Test = NOW
back to the local server. - The local server saves
Test = NOW
to thecookie.txt
file.
- On subsequent runs –
- The local server reads
cookie.txt
and sends it to the remote server. - The remote server now displays the timestamp of the previous visit.
- Then sends set-cookie
Test = NOW
back to the local server. - The local server updates
Test = NOW
in thecookie.txt
file.
- The local server reads
2) MANUALLY SETTING COOKIES
<?php
// (A) INIT CURL - SET URL + RETURN RESULTS & HEADER
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/x-dummy.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// (B) SET COOKIE FILE
$cookie = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt"; // NOTE: ABSOLUTE PATH
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
// CURLOPT_COOKIE - MANUALLY SET COOKIE
curl_setopt($ch, CURLOPT_COOKIE, "keyA=valueA; keyB=valueB");
// (C) CURL FETCH
$result = curl_exec($ch);
if (curl_errno($ch)) { echo curl_error($ch); }
else {
echo $result."\r\n\r\n";
// NOTE: LOCAL COOKIE WILL ONLY SET "TEST"
// SINCE REMOTE SERVER ONLY SPECIFY "SET-COOKIE: TEST=TIMESTAMP"
$clist = curl_getinfo($ch, CURLINFO_COOKIELIST);
echo "LOCAL SERVER COOKIE - ";
print_r($clist);
}
curl_close($ch);
This is the same example as above, but with another line of curl_setopt($ch, CURLOPT_COOKIE)
to append more data. Following up with the above:
- The local server will read from
cookie.txt
, appendkeyA=valueA; keyB=valueB
, and send these to the remote server. - The remote server will now show the cookie values of
Test, keyA, keyB
. - Take note of the response part – The remote server only states set-cookie
Test = NOW
. So the local server will only updateTest
incookie.txt
;keyA keyB
will not be saved.
3) FLUSHING & CLEARING COOKIES
// (C) FORCE NEW SESSION
// CURLOPT_COOKIESESSION - FORCE NEW COOKIE SESSION, IGNORE LAST
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
// CURLOPT_COOKIELIST
// "ALL" ERASES ALL COOKIES IN MEMORY.
// "SESS" ERASES ALL SESSION COOKIES HELD IN MEMORY.
// "FLUSH" WRITES COOKIES TO CURLOPT_COOKIEJAR.
// "RELOAD" RELOAD COOKIES FROM CURLOPT_COOKIEFILE.
curl_setopt($ch, CURLOPT_COOKIELIST, "ALL");
Lastly, just a quick mention – If you need to clear or empty the cookies, use the CURLOPT_COOKIESESSION
or CURLOPT_COOKIELIST
options to do it.
EXTRAS
That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- CURL – PHP
- CURL Options – PHP
- Cookie – PHP
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!