I’m currently using this function to get all my contacts from Google Contacts.
session_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ClientLogin');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array(
'accountType' => 'GOOGLE',
'Email' => 'email',
'Passwd' => 'password',
'source'=>'sourcetest',
'service'=>'cp'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$responses = explode("\n", curl_exec($ch));
$_SESSION['auth'] = str_replace('Auth=', '', $responses[2]);
$_SESSION['email'] = 'email';
$url = 'https://www.google.com/m8/feeds/contacts/default/full';
$url .= '?group=http://www.google.com/m8/feeds/groups/'.$_SESSION['email'].'/base/6';
$url .= '&max-results=500&alt=json';
$ch = curl_init($url);
$header[] = 'Authorization: GoogleLogin auth='.$_SESSION['auth'];
$header[] = 'GData-Version: 3.0';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
curl_close($ch);
To print the whole JSON works fine with echo $respone; but I don’t know how to list all of the contacts (full name, phone and image/avatar) in a table. I have tried json_decode() and even Google’d for a solution for a few days now with no result.
I’m asking you know – how can I fix this?
Thanks in advance.
EDIT
Array
(
[version] => 1.0
[encoding] => UTF-8
[feed] => Array
(
[xmlns] => http://www.w3.org/2005/Atom
[xmlns$openSearch] => http://a9.com/-/spec/opensearch/1.1/
[xmlns$gContact] => http://schemas.google.com/contact/2008
[xmlns$batch] => http://schemas.google.com/gdata/batch
[xmlns$gd] => http://schemas.google.com/g/2005
[gd$etag] => W/"{code}"
[id] => Array
(
[$t] => mymail@gmail.com
)
[updated] => Array
(
[$t] => 2011-11-07T13:50:54.073Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => edgren's Contacts
)
[link] => Array
(
[0] => Array
(
[rel] => alternate
[type] => text/html
[href] => http://www.google.com/
)
[1] => Array
(
[rel] => http://schemas.google.com/g/2005#feed
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full
)
[2] => Array
(
[rel] => http://schemas.google.com/g/2005#post
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full
)
[3] => Array
(
[rel] => http://schemas.google.com/g/2005#batch
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/batch
)
[4] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full?alt=json&max-results=500&group=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2Fmymail%40gmail.com%2Fbase%2F6
)
)
[author] => Array
(
[0] => Array
(
[name] => Array
(
[$t] => edgren
)
[email] => Array
(
[$t] => mymail@gmail.com
)
)
)
[generator] => Array
(
[version] => 1.0
[uri] => http://www.google.com/m8/feeds
[$t] => Contacts
)
[openSearch$totalResults] => Array
(
[$t] => 73
)
[openSearch$startIndex] => Array
(
[$t] => 1
)
[openSearch$itemsPerPage] => Array
(
[$t] => 500
)
[entry] => Array
(
[0] => Array
(
[gd$etag] => "{code}"
[id] => Array
(
[$t] => http://www.google.com/m8/feeds/contacts/mymail%40gmail.com/base/{code}
)
[updated] => Array
(
[$t] => 2011-09-12T17:28:57.835Z
)
[app$edited] => Array
(
[xmlns$app] => http://www.w3.org/2007/app
[$t] => 2011-09-12T17:28:57.835Z
)
[category] => Array
(
[0] => Array
(
[scheme] => http://schemas.google.com/g/2005#kind
[term] => http://schemas.google.com/contact/2008#contact
)
)
[title] => Array
(
[$t] => {contacts name}
)
[link] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/contacts/2008/rel#photo
[type] => image/*
[href] => https://www.google.com/m8/feeds/photos/media/mymail%40gmail.com/{code}
[gd$etag] => "{code}"
)
[1] => Array
(
[rel] => self
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/{code}
)
[2] => Array
(
[rel] => edit
[type] => application/atom+xml
[href] => https://www.google.com/m8/feeds/contacts/mymail%40gmail.com/full/{code}
)
)
[gd$name] => Array
(
[gd$fullName] => Array
(
[$t] => {contacts name}
)
[gd$givenName] => Array
(
[$t] => {contacts last name}
)
[gd$familyName] => Array
(
[$t] => {contacts first name}
)
)
[gContact$birthday] => Array
(
[when] => {contacts birthday}
)
[gd$email] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#other
[address] => {contacts email}
[primary] => true
)
)
[gd$phoneNumber] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#mobile
[$t] => {contacts number}
)
)
[gd$structuredPostalAddress] => Array
(
[0] => Array
(
[rel] => http://schemas.google.com/g/2005#home
[gd$formattedAddress] => Array
(
[$t] => {contacts address}
)
[gd$street] => Array
(
[$t] => {contacts address}
)
[gd$postcode] => Array
(
[$t] => {contacts address}
)
[gd$city] => Array
(
[$t] => {contacts address}
)
[gd$region] => Array
(
[$t] => {contacts address}
)
[gd$country] => Array
(
[code] => SE
[$t] => Sverige
)
)
)
[gContact$website] => Array
(
[0] => Array
(
[href] => {contacts website}
[rel] => profile
)
)
[gd$extendedProperty] => Array
(
[0] => Array
(
[name] => workAddrTB
[value] => WorkAddress2=/WorkCity=/WorkState=/WorkZipCode=/WorkCountry=
)
[1] => Array
(
[name] => homeAddrTB
[value] => HomeAddress2=/HomeCity=/HomeState=/HomeCountry=Sverige/HomeZipCode=
)
[2] => Array
(
[name] => contactTB
[value] => PreferMailFormat=0/allowRemoteContent=
)
)
[gContact$groupMembershipInfo] => Array
(
[0] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/mymail%40gmail.com/base/6
)
[1] => Array
(
[deleted] => false
[href] => http://www.google.com/m8/feeds/groups/mymail%40gmail.com/base/d
)
)
)
Is the JSON that is returned valid? You could paste the result into JSONLint and see if it returns errors.
json_decode()should returnNULLfor invalid input. Try usingvar_dump(json_decode($response). Does it returnNULLor some other value?Also, you should be aware that
json_decode()returns an object by default, so doing something likeecho $response['somekey'];wouldn’t work. To get an associative array, usejson_decode($response, true);.Edit: The
foreachwas just an example. At the time, you didn’t post any output, so I had to guess what would be returned.If you decode the JSON response into an array, you can iterate over the results using
foreach, from your sample, it seems that the contacts are in$the_array["feed"]["entries"].A crude example that should to output a table, if I interpreted the sample correctly:
Note that you can’t use double quotes around the keys (such as $entry[“gd$name”]) unless you escape them like so: $entry[“gd\$name”]. If you use single quotes, you won’t have to do that.
If the API doesn’t always return all fields (for example, if you haven’t filled in a birthdate for the user, the API might return an empty field or it might not return the
gContact$birthdayobject/ array at all. In that case, you’ll need to build in some checks that double-check that a field exists before trying to get the value, of course. Otherwise, you’d get warnings or errors.