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
Submitted by Noob (not verified) on
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
It's not as correct as it is,
Submitted by bao on
It's not as correct as it is, I have tried it before. The correct way is using Yahoo API. Try this: http://developer.yahoo.com/
It's really the easy way
Submitted by Anonymous (not verified) on
It's really the easy way :) Thanks!
Thank you dude ! it's really
Submitted by Anonymous (not verified) on
Thank you dude ! it's really hard to find source up to date about that ! ;)
This is the exact thing that
Submitted by Anonymous (not verified) on
This is the exact thing that I want right now, thank you so much
Nice work bao, it save my lot
Submitted by devender (not verified) on
Nice work bao, it save my lot of time.
Amazing!. I have found a lot
Submitted by Rafael Rend (not verified) on
Amazing!. I have found a lot of scripts that not working.
But this working perfectly.
Now, I need find another script to list from hotmail and yahoo.
Very Thank you!.
Hi your work is really very
Submitted by shiwangi (not verified) on
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
Submitted by Adhaata (not verified) on
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
Submitted by Ankan Bhadra (not verified) on
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.
its good brooo..
Submitted by abhishek (not verified) on
its good brooo..
Can you forward me full code
Submitted by Max (not verified) on
Can you forward me full code to grab gmail contact
Thanks a lot
Submitted by mohamed habeeeb (not verified) on
Thanks a lot
thanks man ... saved my time
Submitted by tariq (not verified) on
thanks man ... saved my time .
GENIUS SCRIPT, it worked the
Submitted by Eric Mulovhedzi (not verified) on
GENIUS SCRIPT, it worked the firstime
Thanks a lot
Awesome! yesterday i searched
Submitted by sud (not verified) on
Awesome!
yesterday i searched allot of script, but no one run perfectly.
thanks, very much!
just want to tell you..
Submitted by phaniraj (not verified) on
just want to tell you..
hats offfff................
thanks alot....
thank you for this Article
Submitted by kranthi (not verified) on
thank you for this Article
Add new comment