2009-10-16 169 views
1

嘿,所有。我一直在尋找一些能夠幫助我解決這個問題的東西。使用cURL自動網站登錄

我正在編寫一個腳本,它將登錄到Gamefly.com,並將50個遊戲添加到我的隊列中。隊列部分工作正常,但登錄根本無法工作。我已閱讀過有關使用cURL的信息,並且我注意到PHP在這方面有足夠的時間。所以這裏是我想出的:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefly.com%2f'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, 'ctl00$ctl00$ctl00$[email protected]xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); 
$test = curl_exec($ch); 

顯然,我想我得到錯誤的領域時,我這樣做。有沒有人有任何想法如何讓它登錄,或者我骨頭?謝謝。

+0

這真的取決於該網站,爲什麼你需要這樣做,如果它是一個值得(非垃圾郵件的原因)檢查與gamefly開發人員,我相信他們會給你一個API使用 – RC1140 2009-10-16 13:53:39

+0

GameFly沒有API。我過去曾問過並提出過懇求。這是一個個人應用程序,我用它來根據標準確定隊列中我想要的遊戲。 – 2009-10-16 13:56:23

回答

4

試試這個:

// Curl.php

class Curl { 

    public $cookieJar = ""; 

    // Make sure the cookies.txt file is read/write permissions 
    public function __construct($cookieJarFile = '/var/www/html/cookies.txt') { 
     $this->cookieJar = $cookieJarFile; 
    } 

    function setup() { 
     $header = array(); 
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
     $header[] = "Cache-Control: max-age=0"; 
     $header[] = "Connection: keep-alive"; 
     $header[] = "Keep-Alive: 300"; 
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
     $header[] = "Accept-Language: en-us,en;q=0.5"; 
     $header[] = "Pragma: "; // browsers keep this blank. 

     curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7'); 
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieJar); 
     curl_setopt($this->curl, CURLOPT_AUTOREFERER, true); 
     curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); 
    } 

    function get($url) { 
     $this->curl = curl_init($url); 
     $this->setup(); 

     return $this->request(); 
    } 

    function getAll($reg, $str) { 
     preg_match_all($reg, $str, $matches); 
     return $matches[1]; 
    } 

    function postForm($url, $fields, $referer = '') { 
     $this->curl = curl_init($url); 
     $this->setup(); 
     curl_setopt($this->curl, CURLOPT_URL, $url); 
     curl_setopt($this->curl, CURLOPT_POST, 1); 
     curl_setopt($this->curl, CURLOPT_REFERER, $referer); 
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); 
     return $this->request(); 
    } 

    function getInfo($info) { 
     $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info); 
     return $info; 
    } 

    function request() { 
     return curl_exec($this->curl); 
    } 
} 

如何撥打:

// login.php中

include('/var/www/html/curl.php'); // This path would change to where you store the file 
$curl = new Curl(); 

$url = "https://www.gamefly.com/member/logon/?ReturnUrl=http%3a%2f%2fwww.gamefly.com%2f"; 
$fields = "ctl00$ctl00$ctl00$[email protected]xxxx.com&ctl00$ctl00$ctl00$MainContent$MainContent$MainContent$LoginForm$password=xxxx"; 

// Calling URL 
$referer = "http://www.gamefly.com"; 

$html = $curl->postForm($url, $fields, $referer); 

echo $html; // This will show you the HTML of the current page you and logged into 
+0

我修改了我的代碼以使用您所寫的內容,但它不起作用。我很肯定在這一點上,當我提交表單或其他類似的東西時,我的字段不正確。我會看看我能否弄清楚。 – 2009-10-16 14:24:55