setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
$client->setClientId('..............');
$client->setClientSecret('..............');
$client->setRedirectUri('............../contacts.php');
$client->setDeveloperKey('...............');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$maxResults = 10;
$contactsGroup = "http://www.google.com/m8/feeds/groups/...email...%40gmail.com/base/......groupid...";
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full".
"?max-results=".$maxResults.
"&group=".$contactsGroup);
$val = $client->getIo()->authenticatedRequest($req);
//reading xml using SimpleXML
$xml= simplexml_load_string($val->getResponseBody());
$xml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$entries = $xml->xpath('//a:entry');
echo "Numf of entries: ".count($entries)."
";
$count = 0;
foreach ($entries as $contact) {
$count++;
$title=$contact->title;
$soci=$contact->content;
$contactNodes=$contact->children('gd', TRUE);
if( $contactNodes->count()>0 ){
$email=$contactNodes->email;
$isPrimaryEmail=$email->attributes()->primary;
$address="";
if($isPrimaryEmail){
$address=$email->attributes()->address;
}
$phoneNumber=$contactNodes->phoneNumber;
}else{
$address="NOT VALID";
}
echo $count.": ".$title.", ".$soci.", email: ".$address.", phone: ".$phoneNumber."
";
}
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$auth = $client->createAuthUrl();
}
if (isset($auth)) {
print "Connect Me!";
} else {
print "Logout";
}