cURLing gmail帐户

时间:2011-01-05 23:58:37

标签: curl gmail

有没有人知道如何格式化curl以便我可以访问我的Gmail并检查是否有新邮件?

P.S。对不起,我忘了提一件大事 - 我用PHP而不是控制台! :(对不起!

3 个答案:

答案 0 :(得分:3)

来自here

curl -u username --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if /<name>/; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'

刚尝试过,它对我有用。 cURL太棒了。

更新:这使用Gmail's atom feed for unread messages。其中使用ssl / https和http身份验证,因此不需要OAuth。

答案 1 :(得分:2)

您可以使用此功能卷曲您的Gmail的RSS Feed / xml

function check_email($username, $password)
{ 
    //url to connect to
    $url = "https://mail.google.com/mail/feed/atom"; 
    // sendRequest 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    $curlData = curl_exec($curl);
    curl_close($curl);

    //returning retrieved feed
    return $curlData;
}

然后,您可以通过从xml中提取值来返回数据。

$em = "youremail@gmail.com";
$pw = "yourpassword";
$feed = check_email($em, $pw);
    $x = new SimpleXmlElement($feed);
    echo "<ul>";
        foreach($x->entry as $msg){
            $href = $msg->link->attributes()->href;
            $qmark = strpos($href,"?")+1;
            $qstring = substr($href,$qmark);

            echo "<li><a href=\"step2.php?".$qstring."\">".$msg->title."</a><br />".$msg->summary."</li>";
        }
    echo "</ul>";

或者只是查看Feed,具体取决于你想要做什么..

$em = "youremail@gmail.com";
$pw = "yourpassword";
$feed = check_email($em, $pw);
echo $feed;

答案 2 :(得分:0)

我接受上一个回答,上面的一个内容确实有效,但您可能需要指定-k才能关闭证书验证。

相关问题