2015-11-26 83 views
0

我试图使用curl登录到ws1.com,但是每当我把POST到真正的我得到错误:错误的请求,这是我试过的代码:登录WS1使用curl

<?php 
$LOGINURL = "https://secure2.ws1.com/login"; 
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "_csrf=QTRvNkJKaUoWBFYDBHkLDHFkP0MdMhAPOUZCASR9Xh4ZRDx7BC8LGA%3D%3D&LoginForm%5Bemail%5D=naczzalid%40hotmail.com&LoginForm%5Bpassword%5D=csc1233&LoginForm%5BrememberMe%5D=0&login-button="); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 
echo $result = curl_exec ($ch); 
curl_close ($ch); 
?> 

请人能向我解释什么是这个问题,所以我可以学习如何做到这一点?

+0

可以给人一种错误的请求。也许如果csrf令牌丢失,它们也会发出错误的请求。确保您提交了所有正确的表单字段和其登录页面所需的任何内容。 – drew010

+0

是的,我敢肯定,我提交的一切,但是当我把帖子= 1我得到这个错误 – SniperCoder

+0

你不把任何带有示例数据后场的所以它很难说。这是要发布的正确URL。也许它也需要一个引用者。你正在发送正确的'_csrf'后置字段值? – drew010

回答

2

我测试了一些东西这种形式,如果csrf码不正确,那么它给出了一个错误的请求。为每个请求和

的CSRF值的变化是依赖于你的cookies。因此,您需要先提取登录页面,然后在提交前提取正确的csrf代码。

工作代码:如果您发布到没有数据页

<?php 
$LOGINURL = "https://secure2.ws1.com/login"; 
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); 
$result = curl_exec ($ch); 

// extract csrf token 
preg_match('/<input type="hidden" name="_csrf" value="([^"]+)">/i', $result, $csrf); 
$csrf = $csrf[1]; 
$csrf = urlencode($csrf); 

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "_csrf={$csrf}&LoginForm%5Bemail%5D=naczzalid%40hotmail.com&LoginForm%5Bpassword%5D=csc1233&LoginForm%5BrememberMe%5D=0&login-button="); 

$result = curl_exec($ch); 

curl_close ($ch); 

var_dump($result); 
+0

非常感谢你,买为什么有时您发送标题和参考,而有时无?当我们需要它们时 – SniperCoder