为什么 php curl 没有在我的 cookiefile 中保存 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19295586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:12:37  来源:igfitidea点击:

Why php curl does not save cookie in my cookiefile?

phpcookiescurlcookiejar

提问by kasper Taeymans

I'm trying to save a cookie in the curl cookiejar. I've simplified my code but its not working.

我正在尝试在 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

设置cookie.php

<?php 
$value = 'something from somewhere';
setcookie("TestCookie", $value);

?>

received header

收到的标题

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

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=某物+来自+某处 内容长度:0 内容类型:文本/html

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!

所以 testcookie 在标题中,但我的 cookiefile 保持为空。我做错了什么?我可以尝试什么来使这个示例工作?谢谢你!

回答by Jeremy Harris

When setting CURLOPT_COOKIEJAR, you need to use an absolute path. You can do this easily by using:

设置时CURLOPT_COOKIEJAR,需要使用绝对路径。您可以使用以下方法轻松完成此操作:

curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file) );

Reference: cannot use cookies in cURL PHP

参考:不能在 cURL PHP 中使用 cookie

回答by Nabi K.A.Z.

As referenceabout CURLOPT_COOKIEJAR:

作为参考有关CURLOPT_COOKIEJAR

The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.

当句柄关闭时保存所有内部 cookie 的文件名,例如在调用 curl_close 之后。

So, in your code didn't used curl_close.

所以,在你的代码中没有使用curl_close.

Use curl_close($ch);after curl_exec, for save cookies in jar file.

使用curl_close($ch);after curl_exec, 将 cookie 保存在 jar 文件中。

回答by Michal - wereda-net

Yes realpath works, but remember that (thanks to php.net) before second call of curl_exec, CURLOPT_COOKIEFILE should be the same as in previous setting of CURLOPT_COOKIEJAR - because it is used for reading from "jared" file :)

是的 realpath 有效,但请记住(感谢 php.net)在第二次调用 curl_exec 之前,CURLOPT_COOKIEFILE 应该与 CURLOPT_COOKIEJAR 的先前设置相同 - 因为它用于从“jared”文件中读取:)

//my working snippet for one php file:
//....
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));
$content = curl_exec($ch);
curl_close($ch); 
$ch = curl_init();  
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('test2-cookie.txt'));
curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath('test2-cookie.txt'));
$content = curl_exec($ch);//second call