Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, Curl, curl_exec(), curl_close() and cookies

Tags:

php

curl

cookies

When using PHP with Curl is it necessary to call curl_close() after every call to curl_exec() for cookies to function properly using the CURLOPT_COOKIEJAR AND CURLOPT_COOKIEFILE options? Or can I call curl_exec() as many times as I like to different urls on the same site and still have the cookies maintained without calling curl_close() after each one? Can I use curl_exec() many times and just close it curl_close() at the end of the script?

like image 377
tom Avatar asked Dec 23 '10 01:12

tom


1 Answers

You should only call curl_close() when you know you're done with that particular handle, or if switching from its current state to a new one (ie: changing a ton of options via curl_setopt() would be faster by going from a clean new handle than your current "dirty" one.

The cookiejar/file options are only strictly necessary for maintaining cookies between seperate curl handles/invokations. Each one's independent of the others, so the cookie files are the only way to share between them.

like image 143
Marc B Avatar answered Oct 11 '22 20:10

Marc B