curl登录Https asp页面

时间:2014-06-06 13:28:22

标签: php curl https asp-classic

我正在尝试使用PHP的https登录页面登录.asp站点。我无法登录该网站。似乎不是生成cookie,查看状态等,也不会将这些参数排除在外。表单字段似乎填写正确(我可以实际看到登录名称)但我不确定密码字段是密码类型,但我认为没有问题,它正确拼写等。

我已尝试过所有相关帖子,包括http://www.mishainthecloud.com/2009/12/screen-scraping-aspnet-application-in.html?showComment=1368565341638#c9104469935977149435

我的代码在下面,并在最后的调用中返回“not Found”(错误代码7,我认为..)。前两次调用没有卷曲错误。

任何人都可以帮忙吗?

$ckfile = tempnam ("/tmp", "CURLCOOKIE"); 

// URL to login page 
$url = "https://secure2.clubwise.com/glenview/memberlogin.asp"; 

// Get Login page and its cookies/ viewstate , etc
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url); 
//curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // no cookie stored 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$output = curl_exec($ch); 
//print(esc_html($output));

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

// rebuild post info -- view state and eventvalidate empty! cant find on page
$fields_string =  
'__VIEWSTATE='.rawurlencode($viewstate).
'&__EVENTVALIDATION='.rawurlencode($eventval).
'&login='.urlencode('xxx@xxx.com'). 
'&password='.urlencode('xxxx').
'&submit='.urlencode('Sign in').
'&redirect='; 

echo $fields_string;

// Post login form -- password field ok?
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 5); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Tells cURL to follow redirects 
$outputb = curl_exec($ch); 
print curl_error;
//var_dump($outputb);


$url = "https://secure2.clubwise.com/glenview/bookclass.asp"; 
$ch = curl_init();  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$outputc = curl_exec($ch); 
print curl_error;
var_dump($outputc);

标题信息:

//$header= array(
//'HTTP/1.1 200 OK',
//'Date: Mon, 09 Jun 2014 00:55:17',
//'GMT Server: Microsoft-IIS/6.0',
//'X-Powered-By: ASP.NET',
//'Content-Length: 21035',
//'Content-Type: text/html',
//'Set-Cookie: ASPSESSIONIDCCSBSTDC=IHJBDOOBIDMJDDOFLAOOBENL; path=/',
//'Cache-control: private'

//);

解决方案:每次操作都是curl_init()导致此操作中断。不需要Viewstate,标头。

$ourFileName = get_stylesheet_directory()."/cookieFile.txt";
$ckfile = $ourFileName; 

// URL to login page 
$url = "https://secure2.clubwise.com/glenview/memberlogin.asp"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$output = curl_exec($ch); 

//Cookie obtained, login 
$fields_string =  
'&redirect='.
'&login='.urlencode('vvv@xxx.com'). 
'&password='.urlencode('xxx').
'&submit='.urlencode('"Sign in"' )
; 

//cookie in the header + header not required
/*
$cookielines =file($ckfile);
foreach($cookielines as $row) {
if($row[0] != '#') {
    $cookie=$row;
    }
}


$header= array( // not needed at moment

'HTTP/1.1 200 OK',
'Date: Mon, 09 Jun 2014 00:55:17',
'GMT Server: Microsoft-IIS/6.0',
'X-Powered-By: ASP.NET',
'Content-Length: 21035',
'Content-Type: text/html',
'Set-Cookie: $cookie; path=/',
'Cache-control: private'

);

*/

$url = "https://secure2.clubwise.com/glenview/memberlogin.asp"; 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 4); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Tells cURL to follow redirects 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING,""); 
curl_setopt($ch, CURLOPT_REFERER,"https://secure2.clubwise.com/glenview/memberlogin.asp");
$outputb = curl_exec($ch); 

$err = curl_errno ( $ch ); echo "<br>error=".$err;
$errmsg = curl_error ( $ch ); echo "<br>errmsg=".$errmsg;
$header = curl_getinfo ( $ch ); echo "<br>header="; var_dump($header);
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); echo "<br>httpcode=".$httpCode;

print curl_error;

//现在您只需要为每次通话添加Cookie,就可以访问密码限制区域内的任何页面:

$url = "https://secure2.clubwise.com/glenview/bookclass.asp?Mode=Area&RecId=67"; 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0 );
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$outputc = curl_exec($ch); 
print curl_error;

var_dump($outputc);

1 个答案:

答案 0 :(得分:1)

请再次检查第一页上存储的Cookie。为会话Cookie设置CURLOPT_COOKIEFILECURLOPT_COOKIEJAR,而不是重新启动或关闭curl以保存会话。 设置CURLOPT_REFERER,某些网站会检查登录页面。