Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
PHP curl / how to ensure cookie file is read?
I've recently discovered that php curl does not load cookies from a cookie file until after at least one request is made. For instance, assume I already have a cookie file from a previous session which contains cookies. This will always return an empty array:
$cookie_file = '/dir/cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$cookies = curl_getinfo($ch, CURLINFO_COOKIELIST);
My workaround is to do something like:
curl_setopt($ch, CURLOPT_URL, "");
curl_exec($ch);
$cookies = curl_getinfo($ch, CURLINFO_COOKIELIST);
But this feels like a hack. Is there a less hacky way to ensure that cookies are loaded into memory for an existing curl session? Alternately, is there any way to determine whether or not they've been loaded?
I want have a method called get_cookies()
but currently it's difficult to know whether or not the cookie file has even been read.
0 comment threads