从我的网站登录gmail


Login to gmail from my site

有很多类似的问题,但是没有一个是正确的。

问题:我有gmail用户名(电子邮件id)和密码,我想登录gmail与重定向页面到gmail.com

I tried:

。获取源代码(从浏览器查看源代码),并将其复制到html页面中。我添加了用户名和密码到这个表单,并提交了这个页面与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    = 'username@gmail.com';
$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    = 'username@gmail.com';
$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。可以从mysite登录到gmail吗?

我不想使用开放ID -这里我需要再次输入用户名和密码。我的用户名和密码已经存储在数据库(我知道这是不安全的。)。所以我的要求是,同时点击链接自动登录到gmail。

问题:2。是否有可能采取采取完整的(我们可以在浏览器"查看源代码"中看到)gmail(gmail登录页面)的源代码,无论是使用php或javascript或jQuery ?

无法使用curl登录gmail -明显的安全问题。至少我不确定是怎么做到的。你必须使用oauth。请参考google oauth文档。这是一个相当标准的过程,需要三次握手。

使用Imap让它工作

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'yourmail@gmail.com';
$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函数

我得到的答案是:

<?php
$USERNAME = 'user@gmail.com';
$PASSWORD = 'pass@gmail';
$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设置(并且不能这样做-同源策略)