php curl 不写入 cookie 文件

php curl does not write cookie file

我知道有很多这样的问题,但谁能帮我缩小问题的范围?

这是我的代码:

ini_set("display_errors", 1);
error_reporting(E_ALL);
$cookies = 'cookies.txt';
$postdatal = array(
    '__VIEWSTATE' => '', // need a __VIEWSTATE, even if it's empty
    'username' => 'username',
    'password' => 'password',
    'button1' => 'Sign+in', // need button1
    '__VIEWSTATEGENERATOR' => '' // same reason as __VIEWSTATE
);

$ch = curl_init();

curl_setopt_array( 
$ch, array(
    CURLOPT_URL => 'url',
    CURLOPT_RETURNTRANSFER => true, // return the results
    CURLOPT_POST => true, 
    CURLOPT_POSTFIELDS => $postdatal, // send the data
    CURLOPT_HEADER => true,
    CURLOPT_COOKIEJAR => $cookies, // set the cookies
    CURLOPT_COOKIEFILE => $cookies 
));

$output = curl_exec($ch);

echo $output;

返回的headers有3个Set-Cookieheaders。有什么办法可以调试这个吗?我似乎没有收到任何错误,即使我选择了一个无效的文件名(cookies.txt 是一个 chmod 777 空文本文件)。

我解决了我的问题:我需要添加 curl_close() 以便保存 cookie 文件。