Skip to content

Instantly share code, notes, and snippets.

@madeinnordeste
Last active August 29, 2015 14:00
Show Gist options
  • Save madeinnordeste/372b0734c7cfb6b4198f to your computer and use it in GitHub Desktop.
Save madeinnordeste/372b0734c7cfb6b4198f to your computer and use it in GitHub Desktop.
PHP - Get Gmail new messages (unread) from Atom Feed
<?php
//PHP - Get Gmail new messages (unread) from Atom Feed
$username = urlencode('my-gmail-account');
$password = 'my-password';
$tag = '';
$handle = curl_init();
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_SSL_VERIFYHOST => '0',
CURLOPT_SSL_VERIFYPEER => '1',
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)',
CURLOPT_VERBOSE => true,
CURLOPT_URL => 'https://'.$username.':'.$password.'@mail.google.com/mail/feed/atom/'.$tag,
);
curl_setopt_array($handle, $options);
$output = (string)curl_exec($handle);
$xml = simplexml_load_string($output);
if (curl_errno($handle)) {
echo 'Error: ' . curl_error($handle);
}
curl_close($handle);
$data = array();
$data['entries'] = array();
$data['title'] = (string)$xml->title;
$data['fullcount'] = (int)$xml->fullcount;
$data['tagline'] = (string)$xml->tagline;
$data['modified'] = (string)$xml->modified;
foreach ($xml->entry as $entry){
$current_entry = array();
$current_entry['author'] = array();
$current_entry['contributor'] = array();
$current_entry['title'] = (string)$entry->title;
$current_entry['summary'] = (string)$entry->summary;
$current_entry['link'] = (string)$entry->link['href'];
$current_entry['modified'] = (string)$entry->modified;
$current_entry['issued'] = (string)$entry->issued;
$current_entry['id'] = (string)$entry->id;
$current_entry['author']['name'] = (string)$entry->author->name;
$current_entry['author']['email'] = (string)$entry->author->email;
$current_entry['contributor']['name'] = (string)$entry->contributor->name;
$current_entry['contributor']['email'] = (string)$entry->contributor->email;
$data['entries'][0] = $current_entry;
}
var_dump($data);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment