1

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

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

我试过了 :

一个。获取源代码(从浏览器查看源代码),并将其复制到 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.

湾。我尝试使用 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. 是否可以使用 php 或 javascript 或 jQuery 获取 gmail 的完整(我们可以在浏览器“查看源代码”中看到)源代码(gmail 的登录页面)?

4

3 回答 3

3

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

于 2013-03-12T05:48:58.127 回答
1

这是我得到的答案:

<?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 邮件的第一页 => 但如果您单击那里的任何链接,它将再次要求登录 =>因为未为域 mail.google.com 设置 cookie(并且无法完成 - 同源策略)

于 2013-03-15T13:28:55.943 回答
1

使用 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 功能

于 2013-03-12T05:48:54.137 回答