2

我正在尝试使用 cURL 登录网站我已经阅读了该网站上的许多答案,但仍然无法获得它。请有人可以帮我找到解决方案

$url = 'https://secure.chess.com/login';

$postfields = "c1=USERNAME&loginpassword=PASSWORD&rememberme=";

$cookie = "cookie.txt";


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
$response = curl_exec($ch);

echo $response;
4

1 回答 1

1

您似乎缺少很多发布变量,其中一个重要的状态散列必须在发布之前获取:

c1:USERNAME
loginpassword:PASSWORD
Qform__FormControl:btnLogin
Qform__FormEvent:QClickEvent
Qform__FormParameter:
Qform__FormCallType:Server
Qform__FormUpdates:
Qform__FormCheckableControls:rememberme
Qform__FormState:9661e98ced2596072a3ca5d70cd57ff5
Qform__FormId:LoginForm

不正确的Qform_FormState绝对不会产生错误消息,这被证明是令人困惑的。您必须首先获取页面,Qform_FormState从 HTML 中提取哈希并提交。还有一个偶尔的c4验证码字段。

我能够毫不费力地做到这一点:

curl https://secure.chess.com/login -v | grep -Po '(?<=Qform__FormState" value=").*(?=")'
# 9b6b7d849c1d4fa16ed25b7b886cf88b
# note Set-Cookie: PHPSESSID=vvtk4stb22e34ac7jhg7m6i142; path=/; domain=.chess.com

curl \
    --header "Cookie: PHPSESSID=vvtk4stb22e34ac7jhg7m6i142" \
    --data="c1=USERNAME&loginpassword=PASSWORD... *snip* ...&Qform__FormState=9b6b7d849c1d4fa16ed25b7b886cf88b ..." \
    https://secure.chess.com/login -v
于 2012-12-07T08:25:31.347 回答