2013-03-12 124 views
1

有很多類似的問題,但沒有一個回答正確。從我的網站登錄gmail

問題:我的Gmail用戶名(電子郵件ID)和密碼,我想登錄使用了重定向頁面gmail.com

我試圖Gmail的:

一個。獲取源代碼(查看源代碼),並將其複製到html頁面, onload我向此表單添加了用戶名和密碼,並使用javascript提交了此頁面 =>它工作正常。

But, I need to fetch the source dynamically, using PHP. 

So, I tried with `file_get_contents` => Here I am not able to get full source code some of the js code and some hidden fields are missing => When I tired to login with this code, it is not logging in, it simply redirects to gmail.com login page. 

Then I tried to get the source code using cURL, This also gave me incomplete source, and I am not able to login. 

b。我試圖用cURL登錄。

我能夠使用下面的代碼獲取Gmail源。

$email = '[email protected]'; 
$password = 'password'; 
$curl  = curl_init('https://mail.google.com/mail/feed/atom'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_USERPWD, $email.':'.$password); 
echo $content = curl_exec($curl); 

但是,當厭倦了代碼登錄到https://accounts.google.com/ServiceLogin它不是在登錄電子

$email = '[email protected]'; 
$password = 'password'; 
$curl  = curl_init('https://accounts.google.com/ServiceLogin'); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl, CURLOPT_USERPWD, $email.':'.$password); 
echo $content = curl_exec($curl); 

問題:1,是否可以登錄到Gmail,從mysite的?

我不想使用開放ID - 在這裏我需要再次輸入用戶名和密碼。我的用戶名和密碼已存儲在數據庫中(我知道這不安全)。所以我的要求是點擊鏈接時自動登錄到Gmail。

問:2.是否有可能採取採取完整的Gmail的源代碼(Gmail中的登錄頁面)(我們可以在瀏覽器的「查看源文件」看到),或者使用PHP或JavaScript或jQuery的?

+1

cURL適合靜態頁面。 Gmail頁面充滿了JS,所以實際上,您將無法檢索完整的內容。你應該有一個上下文來運行JavaScript代碼。 – MatRt 2013-03-12 05:45:11

+1

http://stackoverflow.com/questions/4874186/login-to-site-using-gmail同樣的問題在這裏 你正在做非常不安全的方式來實現這一點。 – chhameed 2013-03-12 05:47:53

+0

@chhameed:我知道,我的問題是不同的,我不想公開ID – 2013-03-12 05:49:05

回答

1

使用IMAP讓這件事的工作

<?php 

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'yourpass'; 


$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

//If connected search for unread mails or do your stuff using imap functions 

$emails = imap_search($inbox,'UNSEEN'); 

if(count($emails) > 0) { 
    foreach($emails as $email) 
    $status = imap_setflag_full($inbox, $email, "\\Seen \\Flagged"); 

echo gettype($status) . "\n"; 
echo $status . "\n"; 
} 

imap_close($inbox); 

?> 

IMAP的參考檢查imap functions

+0

在這裏,你將只能得到Gmail中的內容,但沒有在實際上loged到Gmail ......如果你把鏈接的瀏覽器,它會再次詢問登錄。無論如何+1爲您的努力。 – 2013-03-12 06:17:26

3

這是不可能登錄使用捲曲的Gmail - 明顯的安全問題。至少我不知道如何。你將不得不使用oauth。請參閱google oauth文檔。這是一個相當標準的過程,需要三次握手。

+0

+1 OAuth。只要做到這一點 - 它更容易,更安全。 – 2013-03-12 05:57:56

+0

在這裏我需要再次輸入我的用戶名和密碼......這不是我需要的東西,無論如何+1爲您的努力。 – 2013-03-12 06:17:51

1

這裏是我得到了答案:

<?php 

$USERNAME = '[email protected]'; 
$PASSWORD = '[email protected]'; 
$COOKIEFILE = 'cookies.txt'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLogin'); 
$data = curl_exec($ch); 

//echo $data; 

$formFields = getFormFields($data); 

//print_r($formFields); 

$formFields['Email'] = $USERNAME; 
$formFields['Passwd'] = $PASSWORD; 
unset($formFields['PersistentCookie']); 

$post_string = ''; 
foreach($formFields as $key => $value) { 
    $post_string .= $key . '=' . urlencode($value) . '&'; 
} 

$post_string = substr($post_string, 0, -1); 

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_REFERER, 'https://mail.google.com/'); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 

$result = curl_exec($ch); 
/* 
if (strpos($result, '<title>Redirecting') === false) { 
    die("Login failed"); 
    var_dump($result); 
} else {*/ 
    curl_setopt($ch, CURLOPT_URL, 'https://mail.google.com/mail/h/jeu23doknfnj/?zy=e&f=1'); 
    curl_setopt($ch, CURLOPT_POST, 0); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, null); 

    $result = curl_exec($ch); 
    //header('Location:https://mail.google.com/mail/h/jeu23doknfnj/?zy=e&f=1'); 
    var_dump($result); 
//} 

function getFormFields($data) 
{ 
    if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) { 
     $inputs = getInputs($matches[1]); 

     return $inputs; 
    } else { 
     die('didnt find login form'); 
    } 
} 

function getInputs($form) 
{ 
    $inputs = array(); 

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); 

    if ($elements > 0) { 
     for($i = 0; $i < $elements; $i++) { 
      $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); 

      if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { 
       $name = $name[1]; 
       $value = ''; 

       if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { 
        $value = $value[1]; 
       } 

       $inputs[$name] = $value; 
      } 
     } 
    } 

    return $inputs; 
} 
?> 

它會告訴你的第一頁上的Gmail郵件=>但如果你點擊任何鏈接那裏,要求再登錄=>因爲cookie不是設置爲域mail.google.com(並不能完成 - 相同的原產地政策)