2012-02-26 198 views
0

我努力讓自己在這裏PHP捲曲登錄和獲取數據

https://login.comcast.net/login 

的PHP應用程序,用於登錄Comcast和下載文件

http://xfinity.comcast.net/ 

記住餅乾。

這是我到目前爲止有:

<?php 
$username="username"; 
$password="password"; 
$url="https://login.comcast.net/login"; 
$cookie="cookie.txt"; 

$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/"; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 
curl_close($ch); 
file_put_contents("page.txt",$ch); 

echo $cookie; 
?> 

什麼這個腳本不只是登錄,並告訴我

https://customer.comcast.com/Public/Home.aspx 

因爲我是登錄,然後當我按下電子郵件的例如它簡單地對待我像我從未登錄,要求我的用戶名和密碼。我想要的是通過將http://xfinity.comcast.net/(帶有cookie)保存到page.txt中來獲取電子郵件的數量

這可能嗎?

+0

我有點困惑:首先,您通過CURL將cookie登錄到cookie.txt中,然後嘗試打開_browser_中的頁面,希望它會在cookie.txt中查找cookie ? – raina77ow 2012-02-26 17:39:03

+0

您是否嘗試過使用'CURLOPT_AUTOREFERER'和'CURLOPT_REFERER'選項? – h0tw1r3 2012-02-26 17:39:54

+0

你是對的raina77ow,你有一個點 – Master345 2012-02-26 18:11:14

回答

0

確保設置爲CURLOPT_COOKIEFILE。 Jar是寫入的內容,文件正在發送到服務器。

此外,如果您想在一個請求中執行這兩個操作,則必須查詢服務器兩次。一次登錄(使用CURLOPT_COOKIEJAR),第二次獲取實際內容(使用CURLOPT_COOKIEFILE)。

此外,沒有必要將CURLOPT_FOLLOWLOCATION設置爲false作爲通常的重定向登錄。我會將其更改爲true,只是爲了確保。

+0

我會嘗試所有這些,他們混淆了我的想法...如果你有一些業餘時間,你可以發佈代碼鍵盤上的代碼?不要擔心,我會嘗試這些,我不是無所作爲 – Master345 2012-02-26 18:13:47

+0

@Row sure thing:https://gist.github.com/1918103可能需要一點鍛鍊。 – 2012-02-26 18:21:43

+0

感謝的人...但它只給我的頁面http://xfinity.comcast.net/因爲我沒有登錄...我改變了$ url到$ url_login的方式... – Master345 2012-02-26 18:31:04

0

首先,如果將捲曲線放入一個函數中,將會非常方便。

現在這些步驟應該相當容易。

編輯
確保cookie.txt與您的php文件在同一目錄中。 並確保它是可寫的。 (CHMOD 777) 檢查:

if (is_writable('cookie.txt')) { 
    echo 'The cookie is writable'; 
} else { 
    echo 'The cookie is not writable'; 
    ## start by making it writable : 
    if (! chmod ('cookie.txt', 0777)) 
     die ('chmod() failed on file cookie.txt'); 
} 
  1. 登錄和保存登錄cookie數據與捲曲。 (確保cookie.txt存在並且可寫)。
  2. 做另一個捲曲啓用Cookie並檢索頁面。

事情是這樣的:

//@param string $url : url of page/file 
    //@param bool $binary : binary file. 
    //@param string $post : post data in format : formvar1=VAR1&formvar2=VAR2 
    //@param string $cookie : cookie file. 

function curl($url ,$binary=false,$post=false,$cookie =false){ 

$ch = curl_init(); 

     curl_setopt ($ch, CURLOPT_URL, $url); 
     curl_setopt ($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_REFERER, $url); 
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
     curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 



     if($cookie){ 

      $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)"; 
      curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 

     } 


     if($binary) 
      curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 

     if($post){ 
      curl_setopt($ch, CURLOPT_POST, true); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
     } 

    return curl_exec ($ch); 
} 

請登錄:

$username="username"; 
$password="password"; 
$url="https://login.comcast.net/login"; 
$cookie="cookie.txt"; 

$postdata = "user=".$username."&passwd=".$password."&rm=2&deviceAuthn=false&forceAuthn=true&s=ccentral-cima&r=comcast.net&continue=http://xfinity.comcast.net/"; 

// function 
$ch = curl($url,false,$postdata,$cookie); 

登錄嗎?檢查cookie.txt的內容。

第2步,獲取第二頁。

$url ='http://xfinity.comcast.net/' 
$cookie="cookie.txt"; 

$ch =curl($url,false,false,$cookie); 

echo $ch; 

類似的東西......讓我知道它是否有效。

+0

解析錯誤:語法錯誤,在第3行D:\ xampp \ htdocs \ black \ curl_login \ test_login.php中出現意外的T_STRING ...我從未在PHP中看到過這種類型的編程,這是什麼?!?如果你有時間請幫助我,我真的需要這個 – Master345 2012-02-27 14:21:49

+0

以上4行只是評論..我現在評論他們。 – 2012-02-27 14:53:27

+0

大聲笑,我清除,看看是否其他代碼的作品...它的作品,顯示我http://xfinity.comcast.net/,因爲我沒有登錄... Cookie丟失在某處 – Master345 2012-02-27 14:57:27