Gmail/Gdata Contacts & Group via Proxy

In last few days I got chance to see Gdata API. This is nice. I tried to retrieve Gmail Contacts via Proxy server in PHP code using Zend Gdata libriary. Actually Zend Gdata libriary do not have Contacts object like it has for other Google service like calendar, Youtube etc… But Google contacts can be accessed via Zend_Gdata classes easily on direct internet connection but It has some problem with Proxy connection. It can be solved by providing proxy connection parameters while creating Zend_Http_Client object like following:

$httpProxy = array(
       'adapter'      => 'Zend_Http_Client_Adapter_Proxy',
       'proxy_host'   => 'my.proxy.com',
       'proxy_port'   => 3128
);
$clientProxy = new Zend_Http_Client('https://raiyaraj.wordpress.com/', $httpProxy); 

//Client authentication
$client = Zend_Gdata_ClientLogin::getHttpClient($email, $passwd, 'cp', $clientProxy, 'My-ContactFetcher-0.1'); //cp for contacts

Above code help to do some client authentication via proxy server. But If we use $client object make request to retrieve feed data then it gives some errors saying ‘Can not connect to tcp://google.com:80’. Now it seems we have already provided http proxy detail to $clientProxy object, why it is giving error. But http proxy detail needs to provide again before making request to retrieve feed data. Code is like following

$client->setConfig($httpProxy);

Above $client object can be used to call following functions.

Google contacts can have group so to retrieve associated group of contact. First we need to retrieve all groups & its ids. Because only id of group is stored with contact. Following is code to retrieve all groups & its ids.

function getAllContactsGroups($client,$mail)
{
       $scope= "http://www.google.com/m8/feeds/groups/".urlencode($mail)."/";
       $gdata = new Zend_Gdata($client); 
       $query = new Zend_Gdata_Query($scope.'full');
       $query->setMaxResults(10);
       $feed = $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));
       $arrGroups = array();
       foreach ($feed as $entry){
		$arrGroups[$entry->id->text] =  $entry->title->text;
       }
       return $arrGroups;
} 

Following is code to retrieve all contacts. This code also finds its group id, that is retrieved with contact entry. At the end it prints contact name, its email & its group name.

function getAllContacts($client, $email)
{
	$arrContactGroups = getAllContactsGroups($client,$email);
	$gdata = new Zend_Gdata($client);
	$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/thin');
	$query->setMaxResults(10);
	$feed = $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));
	$i =1;
	$arrContacts =  array();
	foreach($feed as $entry){ 
	$name = $entry->title->text;
	$parts = $entry->getExtensionElements(); 
	$contactGroup = '';
		foreach($parts as $p){ 
			$element = $p->getDOM(); 
			switch($element->tagName){ 
			case 'email': 
				$email = $element->getAttribute('address'); 
				break; 
			case 'groupMembershipInfo': 
				$contactGroup = $element->getAttribute('href'); 
				break; 
			default: 
				continue;
			} 
		}
	echo $i++.") ".$name."=". $email ."=". $arrContactGroups[$contactGroup]."<br/>" ;	
	}
}
Advertisements

2 thoughts on “Gmail/Gdata Contacts & Group via Proxy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s