2011-06-13 73 views
1

我一直在尋找與我曾使用cURL獲取某個網站信息問題的幫助。登入幫助到網站使用PHP +捲曲

我使用curl這樣的新手,所以我需要這個了一些指導。我需要自動登錄到3dstats.com,然後恢復數據列表。列表中沒有問題,我已經在制定解決方案;這是我無法工作的登錄信息。登錄表單,經過多次清理,是這樣的:

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta name="generator" content= 
    "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" /> 
    <title></title> 
    </head> 
    <body> 
    <form action="/cgi-bin/edit2.cgi" method="post"> 
     <input type="hidden" name="type" value="2" /> 
     <input type="text" class="flinput" size="40" name="usr" value="00000000" /> 
     <input type="password" size="40" name="UsrPass" class="flinput" /> 
     <input type="submit" value="Submit " class="binput" /> 
    </form> 
    </body> 
</html> 

所以,我需要發送3個變量,類型,usr和UsrPass。如果我保存此頁面並單擊提交,表單工作正常(在將字段更改爲隱藏並使用正確的登錄值填充它們之後)。 但是,如果我這樣做:

<?php 
    $ch  = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://3dstats.com/cgi-bin/edit2.cgi"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, true); 

    $data = array("type" => "44", 
        "usr"  => "correct8-digitNumber", 
        "UsrPass" => "correctPassword"); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/3dstats/cookies.txt'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/3dstats/cookies.txt'); 

    $output = curl_exec($ch); 
    $info = curl_getinfo($ch); 

    echo "<pre>"; 
    print_r($info); 
    echo "</pre>"; 

    echo $output; 

    curl_close($ch); 
?> 

形式的回報:「錯誤:錯誤賬戶」,用賬號已經填充爲「0000」(注意空格)。該帳戶是一個8位數字。

任何想法,我做錯了嗎?該頁面表示它正在使用Cookie。以後捕獲/使用它們的正確形式是什麼?我正在嘗試的似乎沒有工作。
在此先感謝您的任何幫助/建議。

回答

2

回答我的問題,以供將來參考,它的工作方式是這樣的:

$cookie_file_path = getcwd() . '/cookie.txt'; 
//Emulating Chrome Browser: 
$agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/A.B (KHTML, like Gecko) Chrome/X.Y.Z.W Safari/A.B."; 

/* Login part of the code -- start */ 

//First, get and write session cookie: 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,'http://3dstats.com/cgi-bin/edit2.cgi'); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

$loginpage_html = curl_exec ($ch); 

curl_close ($ch); 

//Now, use the session cookie to actually log in: 
$POSTFIELDS = "type=44&usr=". $your_username ."&UsrPass=". $your_password; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,'http://3dstats.com/cgi-bin/edit2.cgi'); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_REFERER, 'http://3dstats.com/cgi-bin/edit2.cgi'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 

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

登錄現在工作。

0

幾天前我遇到了同樣的問題,我發現解決方案是技術上最簡單最容易處理的問題,就是要求網站所有者製作您嘗試訪問公開展示的網頁。如果您告訴我們該網站是您的網站還是您嘗試從中獲取數據的外部網站,則可以獲得更多信息。

+0

我試圖從中獲取信息的網站不是我的。我正在編寫一個抓取工具,以便從我試圖登錄的網站3dstats.com發佈訪問者信息報告。基本上,我試圖達到的是:使用我的用戶名/密碼登錄到3dstats.com。登錄後,操縱查詢字符串以獲取我需要的報告。然後,獲取HTML並提取製作報告所需的信息。 – kenshin23 2011-06-13 18:33:52