为什么php curl不保存cookie在我的cookiefile? [英] Why php curl does not save cookie in my cookiefile?
问题描述
我试图在curl cookiejar中保存一个cookie。我已经简化了我的代码,但它不工作。
I'm trying to save a cookie in the curl cookiejar. I've simplified my code but its not working.
<?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
setcookie.php
<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
?>
收到标题
HTTP / 1.1 200 OK日期:Thu,10 Oct 2013 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 Content-Length:0 Content-Type:
text / html
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是在标题,但我cookverile保持空。我做错了什么?我可以尝试使这个例子工作吗?谢谢!
so the testcookie is in the header but my cookiefile stays empty. What I'm doing wrong? what can I try to make this example work? thank you!
推荐答案
当设置 CURLOPT_COOKIEJAR
时,绝对路径。您可以使用以下命令轻松完成此操作:
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) );
参考:无法在cURL PHP 中使用Cookie
Reference: cannot use cookies in cURL PHP
这篇关于为什么php curl不保存cookie在我的cookiefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!