加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

为什么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);

?>

收到标题

HTTP/1.1 200 OK Date: Thu,10 Oct 2013 12:10:37 GMT Server:
Apache/2.4.4 (Win64) PHP/5.4.12 X-Powered-By: PHP/5.4.12 Set-Cookie:
TestCookie=something+from+somewhere Content-Length: 0 Content-Type:
text/html

所以testcookie在标题中,但我的cookiefile保持空白.我做错了什么?我可以尝试使这个例子有效吗?谢谢!

设置CURLOPT_COOKIEJAR时,需要使用绝对路径.您可以使用以下方法轻松完成此操作
curl_setopt ($ch,realpath($cookie_file) );

参考:cannot use cookies in cURL PHP

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读