A do-it yourself blog
Import Gmail Address book with Curl and PHP
Hello everyone,
I had to write a class to import a Gmail address book so I decided to share.
As you will see it’s fairly simple.
<?php
class GoogleContact
{
private $email;
private $pass;
private $auth;
public function __construct($email, $pass){
$this->email = $email;
$this->pass = $pass;
}
public function login(){
$url = "https://www.google.com/accounts/ClientLogin";
$post = array("accountType"=>"HOSTED_OR_GOOGLE","Email"=>$this->email,"Passwd"=>$this->pass,"service"=>"cp","source"=>"GoogleContact");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$retour = curl_exec($curl);
if(preg_match("/Error/",$retour)){
return false;
} else {
$auth = explode("Auth=",$retour);
$this->auth = $auth[1];
return true;
}
}
public function fetchContact(){
$authHeader = array('Authorization: GoogleLogin auth='.$this->auth);
$url = "http://www.google.com/m8/feeds/contacts/default/full?max-results=1000";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $authHeader);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$retour = curl_exec($curl);
$matches = array();
preg_match_all("/address='([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})'/i",$retour,$matches);
return $matches;
}
}
?>
And you can use this way :
<?
$g = new GoogleContact("djpate@gmail.com","youwish");
if($g->login()){
$contacts = $g->fetchContact();
print_r($contacts);
} else {
echo "Access denied";
}
?>
Have fun guys !

I have just copy and paste your code and change email and pass but am not getting contacts. i getting this.
Array ( [0] => Array ( ) [1] => Array ( ) )
Please help me.
Thanks
works awesome….what changes would i have to make to have it output name and phone number instead?
Hi Chris,
Got the same error or output: Array ( [0] => Array ( ) [1] => Array ( ) )
Please advice. Thanks. God Bless
I tested from here and It works fine for me.
I tested with two different accounts so I can’t help if I can’t reproduce the bug.
maybe you can try to replace
$retour = curl_exec($curl);
with
$retour = curl_exec($curl);
print_r($retour);
in order to see what info you have and post it back here
hi, many thanks for your code..
Not Working
only output is
Array ( [0] => Array ( ) [1] => Array ( ) )
Can U help me how i get get result
This works! thanks for the code!
Array ( [0] => Array ( ) [1] => Array ( ) ) i Get the same error Chris look at your workin script may be try to be a little more explanatory please
Could you give me the output of print_r($contacts[0]) and print_r($contacts[1]) ?
I’ve just tested it again ( copy pasting from here ) and it works fine for me.
there is error in your code
error is
Array ( [0] => Array ( ) [1] => Array ( ) ) printed on output screen
This code not run properly, it raise an error at line no. 30
Please give me solution on it