看起来网络服务器忽略了我的cookie。 (用于PHP curl)
获取cookie工作正常,但返回不起作用。当我使用浏览器访问网站并删除cookie时,我会得到完全相同的行为。
我该如何解决这个问题?它与https有什么关系吗?
代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/createAccount.jsp');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/var/www/ajax/Cache/Sessions/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/var/www/ajax/Cache/Sessions/cookie.txt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
$data = array('_dyncharset' => 'UTF-8',
'_DARGS' => '/createAccount.jsp',
'firstName' => $firstname,
'login' => $email,
'lastName' => $lastname);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_URL, 'https://example.com/createAccount.jsp?action=createAccount');
curl_setopt($ch, CURLOPT_COOKIEFILE, "/var/www/ajax/Cache/Sessions/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "/var/www/ajax/Cache/Sessions/cookie.txt");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
curl_close($ch);
答案 0 :(得分:2)
确保CURLOPT_COOKIEJAR
文件可由服务器进程写入(执行php脚本)。
如果失败,你可以简单地重复使用相同的curl实例,只需删除
即可curl_close($ch);
$ch = curl_init();
答案 1 :(得分:1)
从第一个curl请求中删除以下行。它应该适合你:
curl_setopt($ch, CURLOPT_COOKIEFILE, "/var/www/ajax/Cache/Sessions/cookie.txt");
CURLOPT_COOKIEFILE
用于从文件向服务器发送cookie。因此,删除此行将意味着不发送cookie。所以服务器不会注意到之前的访问: - )