2014-06-06 188 views
-1

我想用PHP的https登錄頁面登錄到.asp站點。我無法登錄該網站。看起來似乎沒有生成cookie,viewstate等,也沒有將這些參數排除在外。表單域似乎填寫正確(我可以看到登錄名),但我不知道密碼字段是密碼類型,但我不認爲這裏有問題,它的拼寫正確等。curl在Https頁面登錄頁面

我已經嘗試過所有相關帖子,包括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('[email protected]'). 
'&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' 

//); 

SOLUTION:它是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('[email protected]'). 
'&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; 

//現在,你應該能夠通過只包括每個呼叫的餅乾密碼禁區內訪問任何網頁:

$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); 
+0

'$ fields'沒有在'count($ fields)'中定義,因此請嘗試用'5'替換'count($ fields)'' – FuzzyTree

+0

不幸的是,我沒有使用數組來創建查詢字符串因此錯誤:(你不知道你是否可以使用chrome或firebug找到viewstate/eventvalidation?我不使用asp,所以不熟悉它 – David

+0

只是一個提示:我沒有看到普通的瀏覽器頭像Pragme,緩存控制,Referer。也許他們正在檢查這些。我不知道,但它值得一試。 – Sesertin

回答

1

請再次檢查存儲的第一頁上的cookie。對於會話Cookie,請設置CURLOPT_COOKIEFILECURLOPT_COOKIEJAR,並且不要爲保存會話重新啓動或關閉卷曲。 設置CURLOPT_REFERER,一些網站檢查它的登錄頁面。

+0

多數民衆贊成它!這是每個操作curl_init是這個問題,感謝一個工廠:) – David