为什么php curl不会在我的cookiefile中保存cookie?
发布时间:2020-12-13 22:37:50 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试在curl cookiejar中保存一个cookie.我简化了我的代码,但它不起作用. ?php$cookie_file = './cookies.txt';if (! file_exists($cookie_file) || ! is_writable($cookie_file)){ echo 'Cookie file missing or not writable.'; exit;}//cookie_file
我正在尝试在curl cookiejar中保存一个cookie.我简化了我的代码,但它不起作用.
<?php $cookie_file = './cookies.txt'; if (! file_exists($cookie_file) || ! is_writable($cookie_file)){ echo 'Cookie file missing or not writable.'; exit; }//cookie_file is writable,so this is not the issue $ch = curl_init (html_entity_decode("http://localhost/kiala_test/setcookie.php")); curl_setopt ($ch,CURLOPT_COOKIEJAR,$cookie_file); curl_setopt ($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_HEADER,1); //curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); $output = curl_exec ($ch); echo $output; ?> setcookie.php <?php $value = 'something from somewhere'; setcookie("TestCookie",$value); ?> 收到标题
所以testcookie在标题中,但我的cookiefile保持空白.我做错了什么?我可以尝试使这个例子有效吗?谢谢!
设置CURLOPT_COOKIEJAR时,需要使用绝对路径.您可以使用以下方法轻松完成此操作
curl_setopt ($ch,realpath($cookie_file) ); 参考:cannot use cookies in cURL PHP (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |