Created
October 16, 2010 20:31
-
-
Save lardissone/630233 to your computer and use it in GitHub Desktop.
Access Google Contacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/************************ | |
* Google Contact importer | |
* | |
* Asks for permissions to provide access to your Gmail/Google contacts | |
* to the given site. | |
*************************/ | |
// include all required components from Zend Gdata | |
// from: http://framework.zend.com/download/gdata | |
require_once 'Zend/Loader.php'; | |
Zend_Loader::loadClass('Zend_Gdata'); | |
Zend_Loader::loadClass('Zend_Gdata_AuthSub'); | |
Zend_Loader::loadClass('Zend_Gdata_Query'); | |
Zend_Loader::loadClass('Zend_Uri_Http'); | |
session_start(); | |
// I'm based this function from the original by Eric (Google) | |
// from his blog: http://bit.ly/9sKIft | |
function setupClient($singleUseToken = null) { | |
$client = null; | |
// Fetch a new AuthSub token? | |
if (!$singleUseToken) { | |
$next = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | |
$scope = 'http://www.google.com/m8/feeds'; | |
$secure = 1; // <-- important to get secure connection | |
$session = 1; | |
$permission = 1; // 1 - allows posting notices && allows reading profile data | |
$authSubURL = Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, $session); | |
$authSubURL .= '&permission=' . $permission; | |
header('Location: '. $authSubURL); | |
} else { | |
$client = new Zend_Gdata_HttpClient(); | |
// This sets your private key to be used to sign subsequent requests | |
$key_file = '/your/absolute/path/to/yourkey.pem'; | |
$client->setAuthSubPrivateKeyFile($key_file, null, true); | |
$sessionToken = Zend_Gdata_AuthSub::getAuthSubSessionToken(trim($singleUseToken), $client); | |
// Set the long-lived session token for subsequent requests | |
$client->setAuthSubToken($sessionToken); | |
} | |
return $client; | |
} | |
$client = setupClient(@$_GET['token']); | |
// if we already have a client setup | |
if ($client) { | |
$gdata = new Zend_Gdata($client); | |
// query the Google contacts | |
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full'); | |
$feed = $gdata->getFeed($query); | |
$entries = $gdata->retrieveAllEntriesForFeed($feed); | |
$contacts = array(); | |
// in this example we only display contacts with email addresses | |
foreach ($entries as $entry) { | |
$name = $entry->title->text; | |
$output = '<br />Name: '. $name; | |
$email = null; | |
$extensions = $entry->getExtensionElements(); | |
foreach ($extensions as $extension) { | |
if ($extension->rootElement == 'email') { | |
$attributes = $extension->getExtensionAttributes(); | |
if (!isset($email)) $output .= ' - Email: '. $attributes['address']['value']; | |
$email = $attributes['address']['value']; | |
} | |
} | |
if ($email) { | |
print $output; | |
$contacts[] = array('name' => $name, 'email' => $email); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment