Enumerate Gmail Contacts

October 2, 2009

Here’s some sample code I’ve piled together to list gmail contacts:

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Gdata_Query');

$user = 'user@gmail.com';
$pass = 'password';

// Using Client Login
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, 'cp');
$gdata = new Zend_Gdata($client);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/'.$user.'/full');
$query->setMaxResults(100);
$feed = $gdata->getFeed($query);

$xml = new SimpleXMLElement($feed->getXML());
$entries = $xml->children('http://www.w3.org/2005/Atom');

foreach ($entries->entry as $entry )
{
	$defaults = $entry->children('http://schemas.google.com/g/2005');
	echo 'title: '. $entry->title."\n";
	if(isset($defaults->email))
	{
		echo ' email: '.$defaults->email->attributes()->address."\n";
	}
	echo ' address: '.$defaults->postalAddress."\n";
	echo "\n";
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive