Why php curl does not save cookie in my cookiefile? Why php curl does not save cookie in my cookiefile? curl curl

Why php curl does not save cookie in my cookiefile?


When setting CURLOPT_COOKIEJAR, you need to use an absolute path. You can do this easily by using:

curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );

Reference: cannot use cookies in cURL PHP


As reference about CURLOPT_COOKIEJAR:

The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

So, in your code didn't used curl_close.

Use curl_close($ch); after curl_exec, for save cookies in jar file.


Yes realpath works, but remember that (thanks to php.net) before second call of curl_exec, CURLOPT_COOKIEFILE should be the same as in previous setting of CURLOPT_COOKIEJAR - because it is used for reading from "jared" file :)

//my working snippet for one php file://....curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));$content = curl_exec($ch);curl_close($ch); $ch = curl_init();  curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath('test2-cookie.txt'));$content = curl_exec($ch);//second call