php curl POST登录asp.net

时间:2014-01-11 21:46:08

标签: php asp.net curl

我正在尝试使用cURL自动登录此网站:GSC。该站点使用ASP.NET构建。我首先执行GET请求以获取会话ID,这很好。然后,我需要向同一站点发送POST请求,其中包含会话ID,用户名,密码和一些登录位置。登录位置根据浏览器的窗口大小而变化,所以我只选择一些随机的。我已经留下了下面的用户名和密码,但我已经多次检查过它们并且是正确的。在Chrome中我可以看到还有一个查询字符串参数,但我不确定我是否应该将其包含在任何地方?我在Chrome浏览器中手动添加了请求+响应的两张图片以及我用于自动登录的php脚本。如果我犯了任何错误,有人可以看到吗?

请求+回复: First part of the request+response from Chrome console

Second part of the request+response from Chrome console

PHP脚本:

function get_headers_from_curl_response($headerContent)
{

    $headers = array();

    // Split the string on every "double" new line.
    $arrRequests = explode("\r\n\r\n", $headerContent);

    // Loop of response headers. The "count() -1" is to 
    //avoid an empty row for the extra line break before the body of the response.
    for ($index = 0; $index < count($arrRequests) -1; $index++) {

        foreach (explode("\r\n", $arrRequests[$index]) as $i => $line)
        {
            if ($i === 0)
                $headers[$index]['http_code'] = $line;
            else
            {
                list ($key, $value) = explode(': ', $line);
                $headers[$index][$key] = $value;
            }
        }
    }

    return $headers;
}
function regexExtract($text, $regex, $regs, $nthValue)
{
    if (preg_match($regex, $text, $regs)) {
        $result = $regs[$nthValue];
    }
    else {
         $result = "";
    }
return $result;
}
$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
$regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';

$ch = curl_init("http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');

$response = curl_exec($ch);
curl_close($ch);

$viewstate = regexExtract($response,$regexViewstate,$regs,1);
$eventval = regexExtract($response, $regexEventVal,$regs,1);

$params = array(
    '__EVENTTARGET' => '',
    '__EVENTARGUMENT' => '',
    '__VIEWSTATE' => $viewstate,
    '__EVENTVALIDATION' => $eventval, 
    'ctl00%24txtUsername' => 'xxx',
    'ctl00%24txtPassword' => 'xxx',
    'ctl00$ImgLogin.x' => '0',
    'ctl00$ImgLogin.y' => '0',
);

$ch2 = curl_init("http://gsc.klub-modul.dk/cms/ShowContentPage.aspx?ContentPageID=1");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HEADER, 1);
curl_setopt ($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt ($ch2, CURLOPT_COOKIE,'cookies.txt');
curl_setopt($ch2,CURLOPT_COOKIEJAR,'cookies2.txt');

$response2 = curl_exec($ch2);
curl_close($ch2);

foreach(get_headers_from_curl_response($response2) as $value)
{
    foreach($value as $key => $value2)
    {
        echo $key . ": " .$value2 . "<br />";
    }
}

1 个答案:

答案 0 :(得分:2)

你的代码看起来很干净,除非你在其他地方没有犯错误。只需要修理一件事:

'ctl00$txtUsername' => 'xxx',
'ctl00$txtPassword' => 'xxx',

您在密钥中使用了%24,这些密钥由函数http_build_query()

进一步进行了urlencoded