cURLing gmail account
Does anyone know how to format curl so i can access my gmail and check if there's some new mail?
P.S. I'm sorry, I forgot to mention one huge thing - i'm using PHP, not console! :( 开发者_开发百科Sorry!
From 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>/;'
Just tried it out and it worked for me. cURL is awesome.
Update: this uses Gmail's atom feed for unread messages. Which uses ssl/https and http authentication so no OAuth necessary.
You can curl your gmail's rss feed/xml with this function
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;
}
Then you can return your data either by extracting values from the 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>";
Or by just viewing the feed, depending on what you want to do with it..
$em = "youremail@gmail.com";
$pw = "yourpassword";
$feed = check_email($em, $pw);
echo $feed;
I take my previous answer back, the one liner above does work, although you might need to specify -k in order to turn off certificate verification.
精彩评论