- PHP CURL With Cookies (Simple Examples)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- PHP CURL COOKIES
- 1) SET & GET COOKIE
- 1A) LOCAL SERVER CURL CALL
- 1B) DUMMY REMOTE SERVER
- 1C) WHAT WILL HAPPEN
- 2) MANUALLY SETTING COOKIES
- 3) FLUSHING & CLEARING COOKIES
- DOWNLOAD & NOTES
- SUPPORT
- EXAMPLE CODE DOWNLOAD
- EXTRA BITS & LINKS
- LINKS & REFERENCES
- INFOGRAPHIC CHEAT SHEET
- THE END
- Leave a Comment Cancel Reply
- Search
- Breakthrough Javascript
- Socials
- About Me
- PHP cURL with Sessions
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!
TLDR – QUICK SLIDES
TABLE OF CONTENTS
PHP CURL COOKIES
All right, let us now get into the examples of handling cookies with PHP CURL.
1) SET & GET COOKIE
1A) LOCAL SERVER CURL CALL
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
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 the cookie.txt file.
- 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
- 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
// (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.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a «paid scripts and courses» business, so every little bit of support helps.
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, 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.
EXTRA 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.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET
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 Cancel Reply
Search
Breakthrough Javascript
Take pictures with the webcam, voice commands, video calls, GPS, NFC. Yes, all possible with Javascript — Check out Breakthrough Javascript!
Socials
About Me
W.S. Toh is a senior web developer and SEO practitioner with over 20 years of experience. Graduated from the University of London. When not secretly being an evil tech ninja, he enjoys photography and working on DIY projects.
Code Boxx participates in the eBay Partner Network, an affiliate program designed for sites to earn commission fees by linking to ebay.com. We also participate in affiliate programs with Bluehost, ShareASale, Clickbank, and other sites. We are compensated for referring traffic.
PHP cURL with Sessions
PHP scripts have the ability to request content from other servers using cURL. cURL makes a request to a remote server page, fetches the content and returns it to the script. cURL can make GET and POST requests, retrieving and pushing data respectively, behaving very much like a browser client making requests to a website.
Web browsers employ sessions, however, that provide some kind of memory between requests as to the client’s interactions with the server. Session cookies store information the server instructs the client to store, and these properties are then sent back to the server in the following requests. The server will very often use these to identify a client or provide it with a different experience in terms of page layout or available services.
To emulate this process in PHP with cURL, we can use cURL’s cookies. Take a standard PHP cURL request:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $content = curl_exec($ch); curl_close($ch);
This code will fetch the remote page using a POST request, but it makes no use of cookies so no session will be kept. To allow cURL to store and send cookies when contacting this server, we’d need to specify a cookie file:
$cookieFile = "cookies.txt"; if(!file_exists($cookieFile))
The file must be writeable by the web server of course. Once it’s accessible, adding a couple of new lines to the topmost example will yield cookie functionality:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // Cookie aware curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // Cookie aware $content = curl_exec($ch); curl_close($ch);
That’s it – all cURL requests using this code will store and send cookies.