0

我正在尝试使用 php curl 登录一个站点,但我不熟悉 csrf_token,我在我的帖子数据中看到我也在发送这个。以下是我的代码,我无法登录。我的浏览器发送的 csrf_token 是:

csrf_token=20131028152953%23%23f31508fbc631e906973451eefe8a88ce1b9270ad&name=name&password=pass

$name="name"; 
$password="pass"; 
$url="domain.com/login"; 
$cookie="cookie.txt"; 

$postdata = "name=".$name."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  

curl_close($ch);
4

1 回答 1

0

我猜csrf_token每次都在改变,你加载网站。您应该首先加载站点,登录表单在哪里,然后在 DOM 中解析csrf_token,然后按照问题中的内容执行 curl。

于 2013-10-28T13:59:53.233 回答