Php 使用curl登录到WS1

Php 使用curl登录到WS1,php,http,curl,libcurl,php-curl,Php,Http,Curl,Libcurl,Php Curl,我正在尝试使用curl登录ws1.com,但是每当我将帖子设置为true时,我都会收到错误:请求不正确,这是我尝试的代码: <?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($c

我正在尝试使用curl登录ws1.com,但是每当我将帖子设置为true时,我都会收到错误:请求不正确,这是我尝试的代码:

<?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);
?>


任何人都可以向我解释一下这里面的问题是什么,这样我就可以学会怎么做了?

我用这个表单测试了一些东西,如果
csrf
代码不正确,那么它会给出一个错误的请求

csrf值会因每个请求而改变,并与您的cookie相关联。因此,您需要首先获取登录页面,并在提交之前提取正确的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);

如果您在没有数据的情况下发布到该页面,则可能会发出错误的请求。也许如果csrf令牌丢失,他们也会发出错误的请求。请确保您提交了所有正确的表单字段以及登录页面所需的任何其他内容。是的,我确定我已提交了所有内容,但是当我将post=1时,我收到了此错误。您没有将任何post字段与示例数据放在一起,因此很难判断。但这是要发布到的正确URL。也许它也需要一个推荐人。您发送的是正确的
\u csrf
post字段值吗?我想是的,我只是更新了代码以查看它。非常感谢,为什么有时发送标题和引用,有时不发送?什么时候我们需要他们