用curl登录

时间:2012-04-03 00:27:40

标签: php curl login

我正在尝试使用以下cURL操作登录bangme.net图片评级网站。当我检查登录表单时,ID是signin_UserName,signin_UserPass。名称是$ UserName,登录$ UserPass。该脚本似乎不适用于下划线,因为结果将我引导到一个页面,要求我登录,据我所知。我无法使用美元符号名称,因为它将其转换为变量。我注意到一些这样的例子,类似的脚本显示了一些以'&'开头的字段。当他们想要登录的网页上的表单上没有该标志时签名。这是做什么的?您可以提供的任何帮助将不胜感激。谢谢。

$username = "username";
$password = "password";
$url = "http://www.bangme.net/starform.aspx";
$cookie = "cookie.txt";

$postdata = "signin_UserName=" .$username . "signin_UserPass=" .$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, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $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);

新代码:

$username = "username";
$password = "password";
$url = "http://www.bangme.net/signin.aspx";
$cookie = "cookie.txt";

$post = array(
        '__EVENTTARGET'=>"" ,
        '__EVENTARGUMENT'=>"",
        '__VIEWSTATE'=>"/wEPDwUJMzQ0MjU1MjA0ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUJbGFzdHBob3RvBQh2b3RleWVzMgUHdm90ZW5vMgfkai25gOMuDdbmzYMWHA2LgYKt",
        '__EVENTVALIDATION'=>"/wEWDgLmnpGMBwKOwMbqCwK4wb4IAv/10agEAqHH7a8KAtXfk+ULAt/Q94gCAvS2udYOAqrdhtcHAq+k5d0LAo+Au5MKApm/3+0OAqDCuuYMArCmgTeosqrEgmB3P5bavC40rTc6dbeF1g==",
        'signin$UserName'=>$username ,
        'signin$UserPass'=>$password ,
        'signin$Button1.x'=>42,
        'signin$Button1.y'=>17
        );

$postData = http_build_query($post);

$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);//changed from 0 to 1
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//new
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//new


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

echo $result;
curl_close($ch);

3 个答案:

答案 0 :(得分:2)

我在这看错了3件事

一个。错误的变量声明......

更正

 $post = 'signin$UserName=' . $username . '&signin$UserPass=' . $password;

B中。错误的网址

更正

 http://www.bangme.net/signin.aspx

℃。参数未完成系统预计如下:

编辑1

$post = array(
        '__EVENTTARGET'=>"" ,
        '__EVENTARGUMENT'=>"",
        '__VIEWSTATE'=>'/wEPDwULLTEyMzUzNDExNjFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRdzaWduaW4kUmVtZW1iZXJDaGVja2JveAUOc2lnbmluJEJ1dHRvbjFA3HLFp6KEVYvii7p6PjecyYMsaw==',
        '__EVENTVALIDATION'=>'/wEWBQL/xuPVDgLCoo75CQKoiOf4DQKtwMGFDgLz0rmTDUnuxoGtW34gNpQqIpwh3OT11JJl',
        'signin$UserName'=>$username ,
        'signin$UserPass'=>$password ,
        'signin$Button1.x'=>42,
        'signin$Button1.y'=>17
        );
$postData = http_build_query($post);

修改2

Full Headers ...只是注意到它也有饼干

POST http://www.bangme.net/signin.aspx HTTP/1.1
Host: www.bangme.net
Connection: keep-alive
Content-Length: 391
Cache-Control: max-age=0
Origin: http://www.bangme.net
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.bangme.net/signin.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=1jqjwt45h0gpzb452rvrcu55; __utma=178887351.705741064.1333412926.1333412926.1333412926.1; __utmc=178887351; __utmz=178887351.1333412926.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utmb=178887351

__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTEyMzUzNDExNjFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRdzaWduaW4kUmVtZW1iZXJDaGVja2JveAUOc2lnbmluJEJ1dHRvbjFA3HLFp6KEVYvii7p6PjecyYMsaw%3D%3D&__EVENTVALIDATION=%2FwEWBQL%2FxuPVDgLCoo75CQKoiOf4DQKtwMGFDgLz0rmTDUnuxoGtW34gNpQqIpwh3OT11JJl&signin%24UserName=test&signin%24UserPass=test&signin%24Button1.x=42&signin%24Button1.y=17

编辑3

更改

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

由于

:)

答案 1 :(得分:1)

您需要使用带有美元符号的表单字段名称;你需要转义$以使其不被解释为变量,或者将其粘贴在单引号中,因此它只被评估为纯文本。

$post = 'signin$UserName=' . $username . '&signin$UserPass=' . $password;
// or
$post = "signin\$UserName=$username&signin\$UserPass=$password";

试一试,看看你是否取得了进展。使用Wiresharklivehttpheaders等内容在您的浏览器中嗅出请求,以帮助您查看提交数据的确切格式。

另见PHP - Strings - Single Quoted

答案 2 :(得分:0)

据我所知,你在两个帖子参数之间错过了&符号(&)。

要处理输入字段中的$,只需在代码中使用\$(转义美元符号)或将带有$符号的部分放入单引号中。 (但是你必须将所有变量引用放在字符串之外并用.连接它们)