The easy way to get GMail contact list

If you're looking for a solution to get contact list / address book in Gmail, you're in the right place. I was looking for the solution too. Actually, I also want to grab contact list from other popular service such as Live, Yahoo! Mail, etc ... but I started with Gmail first. The requirement is simple: the user provides email and password, the application will perform some requests to google and return the contact list.

After doing a few googles, I found some scripts from phpclasses and sourceforge that provide the feature but, sadly, all of them are outdated and do not work anymore. Torrential Web Dev has solved our problem, but the author's script is outdated too. In short, there is no easy working example at the moment. Google API has a tutorial but in Java language, we can do the same in PHP with Zend GData, but I don't want to bring that big heavy thing in the code. So I re-wrote everything in simple PHP based on Google Account API and Google Contact API, and here is the result.

<?php
 
error_reporting(E_ALL);
 
$user = "email";
$password = "password";
 
// ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
 
// step 1: login
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
	'Email' => $user,
	'Passwd' => $password,
	'service' => 'cp', // <== contact list service code
	'source' => 'test-google-contact-grabber',
	'accountType' => 'GOOGLE',
);
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl);
 
$returns = array();
 
foreach (explode("\n",$result) as $line)
{
	$line = trim($line);
	if (!$line) continue;
	list($k,$v) = explode("=",$line,2);
 
	$returns[$k] = $v;
}
 
curl_close($curl);
 
// step 2: grab the contact list
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";
 
$header = array(
	'Authorization: GoogleLogin auth=' . $returns['Auth'],
);
 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feed_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
 
$result = curl_exec($curl);
curl_close($curl);
 
$data = json_decode($result);
 
$contacts = array();
 
foreach ($data->feed->entry as $entry)
{
	$contact = new stdClass();
	$contact->title = $entry->title->{'$t'};
	$contact->email = $entry->{'gd$email'}[0]->address;
 
	$contacts[] = $contact;
}
 
var_dump($contacts);

This script just works and gives you the basic idea how to fetch the google contact list. You can rewrite it so it can be reused easier. I will take a look on Yahoo and Live too, and surely I will share the solution here when I find them.

18 Comments

http://www.mediafire.com/?wkm

http://www.mediafire.com/?wkmux3znmnj

I found this code when dealing with my project. Really don't remember where i got it, but it is good. It can grab Yahoo Messenger List, Y! Contact, Y! Address Book, Check New Mail. The works are simple as cURL does almost everything :-D

Hi your work is really very

Hi your work is really very good but i am not able to fetch the contacts by this code. i have putted my username and password inside the $user and $password variable but its showin error like this...

object(GMailSnapshot)[2]
public 'created' => int 0
public 'snapshot_error' => string 'Invalid response from Gmail (empty)' (length=35)

object(GMailSnapshot)[2]
public 'created' => int 0
public 'snapshot_error' => string 'Invalid response from Gmail (empty)' (length=35)

For all who facing in error

For all who facing in error handling with gd$email

foreach ($data->feed->entry as $entry)
{
if(isset($entry->{'gd$email'}[0]->address)){

$contact = new stdClass();
$contact->title = $entry->title->{'$t'};
$contact->email = $entry->{'gd$email'}[0]->address;

$contacts[] = $contact;
}
}

var_dump($contacts);

For gmail, this script if

For gmail, this script if fine. But for yahoo, hotmail this script does not run.What is best solution to get yahoo, hotmail contact list with CURL and php like gmail script.

Add new comment