我正在尝试将cookie保存在curl cookiejar中。我简化了代码,但无法正常工作。
<?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日期:2013年10月10日,星期四12:10:37 GMT服务器:Apache / 2.4.4(Win64)PHP / 5.4.12 X-Powered-By:PHP / 5.4.12 Set-Cookie:TestCookie = something + from + somewhere内容长度:0内容类型:text / html
所以testcookie在标题中,但我的cookiefile保持为空。我做错了什么?我怎样才能使该示例正常工作?谢谢你!
设置时CURLOPT_COOKIEJAR
,您需要使用绝对路径。您可以使用以下方法轻松完成此操作:
curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句