Why php curl does not save cookie in my cookiefile?

假如想象 提交于 2019-11-30 18:46:13
Jeremy Harris

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!