PHP CURL With Cookies – Simple Examples

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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes CURL Cookies Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

 

1) SET & GET COOKIE

LOCAL SERVER CURL CALL

1-cookie-file.php
<?php
// (A) INIT CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/0-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.

DUMMY REMOTE SERVER

0-dummy.php
<?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.

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 the cookie.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 the cookie.txt file.

 

 

2) MANUALLY SETTING COOKIES

2-cookie-set.php
<?php
// (A) INIT CURL - SET URL + RETURN RESULTS & HEADER
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/0-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, append keyA=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 update Test in cookie.txt; keyA keyB will not be saved.

 

 

3) FLUSHING & CLEARING COOKIES

3-cookie-erase.php
// (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.

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

PHP CURL With Cookies (click to enlarge)

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *