2012-01-23 59 views
2

這裏的登錄頁面:如何捲曲需要登錄一個頁面第一

http://www.ifreewind.net/iFreeWind.aspx 

我需要這個頁面的內容,其需要先登錄:

http://www.ifreewind.net/Users/Search.aspx?R=1&P=00102&i=2 

在您需要的情況下,我的帖子數據內容是在這裏:

$data = "__VIEWSTATE=%2FwEPDwULLTE3NjQ3MDc3NDQPZBYCAgMPZBYCAgEPFgIeB1Zpc2libGVoZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUSUmVtZW1iZXJNZUNoZWNrQm94r57YdIUtbSps%2FGLW1PUtjxcILdE%3D&__EVENTVALIDATION=%2FwEWBQLKivfjBgLw2N3fDgLC9%2FChAwLxuKbKAgL%2BjNCfDwU6DJjH4Q2acTlGVXmDrSv2Nn4G&UserNameTextBox=myemailaddress%40gmail.com&PasswordTextBox=mypassword&LoginButton=%E7%99%BB%E9%99%86"; 
curl_setopt($datapost, CURLOPT_POSTFIELDS, $data); 

我的代碼是在這裏,但不工作:

$site = "http://www.ifreewind.net/Users/Search.aspx?R=1&P=00102&i=2"; 
$ch = curl_init(); 
$headers = array('Host:www.ifreewind.net', 
     'User-Agent:Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1', 
     'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 
     'Accept-Language:zh-cn,zh;q=0.5', 
     'Accept-Encoding:gzip, deflate', 
     'Accept-Charset:GB2312,utf-8;q=0.7,*;q=0.7', 
     'Connection:keep-alive', 
     'Referer:http://www.ifreewind.net/Users/Search.aspx?R=1&P=00102&i=2', 
     'Cookie:Hm_lvt_7fa3bcf45d96b91c6a87d1433c045849=1327324205986; VisitUrl_-1=ok; ASP.NET_SessionId=4smyrujt3m3cnu2sxbh55z55; Hm_lpvt_7fa3bcf45d96b91c6a87d1433c045849=1327324205986; MyId=7087; iTechAuthen=FDDE38649ADA11A5C73923D4D9437097226833721D48739F39720A91C49A95DCC345C8E9DE670B71D837808619CBF23213C6252AE82112A06CE37271D7D1A3466979E2B264845C8C75B7E1791DDB49C910178DA0BC6D5BD4D6AC536842279D41FA2866DA5B4F278BAB6443D2F370B96F1E5723C685AA015BE611317F40F66965DD2CF0FD5E7C1DB794D7172CC784EF1C2B773CFCAE05772EE611B6F82EF6894F8B32EA932D01F81F70F73C18F1CB8C6F3DDC5E44', 
     'Cache-Control:max-age=0' 
     ); 


curl_setopt($ch, CURLOPT_URL, $site); 
curl_setopt($ch, CURLOPT_TIMEOUT, 6000); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 

//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($ch, CURLOPT_POST, TRUE); 
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//add 

ob_start(); 
return curl_exec($ch); 

ob_end_clean(); 
curl_close($ch); 
unset($ch); 

其實我的代碼會在網頁瀏覽器中遇到很多「 ܰ 」奇怪的結果,我試着在Firefox中切換語言字符集,但是utf-8和其他程序並沒有表現出色,所以請幫助。

回答

2

您可以使用頭解析器來取回PHPSESSID。這使得未來的捲曲請求更清晰。下面是一個例子。 這是一些面向對象編程的一部分,我可以將CURL捲入站點的不同部分,併發送我的會話ID來跟蹤會話。

function login($email,$password){ 
$login=false; 
$post='member[email]='.urlencode($email).'&member[password]='.urlencode($password); 
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, 'http://signon.page/login.php'); 
curl_setopt($ch,CURLOPT_USERAGENT,$this->useragent); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
//POST 
curl_setopt($ch,CURLOPT_POST,4); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); 

curl_setopt($ch, CURLOPT_HEADER, 1); 
$output=curl_exec($ch); 

//get cookies in map array 
$rows=explode("\n",$output); 
foreach($rows as $num=>$row){ 
$trim=substr($row,0,5); 
$trim2=substr($row,0,29); 
if ($trim2=="Location: /public/member/home")$login=true; 
/* if the site sends back a header redirect my login worked.*/ 
if ($trim=="Set-C") {$rownum=$num;}} 
$cookies=$rows[$rownum]; 
$cookies=substr($cookies,12);/*RAW COOKIE*/ 
$cookies=explode("; ",$cookies); 
$arr=array(); 
foreach ($cookies as $n=>$v){ 
$s=explode("=",$v); 
$arr[$s[0]]=$s[1];} 
$cookies=$arr; 

$_SESSION['SN']=$cookies['PHPSESSID']; 
curl_close($ch); 
$_SESSION['auth']=$login; 
return $login;}//end isLoggedIn 
+0

謝謝你,我搜索了一整天關於PHPSESSID,但仍然困惑它。所以我仍然沒有明白的代碼。也許我應該買一本書。 – Phoenix

+0

請原諒我現在缺乏壓痕 –