使用Google Contacts API-PHP检索电话号码

时间:2014-08-05 08:44:57

标签: php google-api

我想使用Google通讯录API获取我的Google通讯录的电话号码。 但我在互联网上找不到任何提示! 请帮我! 我只能收到我的联系人的电子邮件和姓名。 (我是一名Php程序员)

1 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助:)



function curl_file_get_contents($url)
{
	$curl = curl_init();
	$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

	curl_setopt($curl,CURLOPT_URL,$url);	//The URL to fetch. This can also be set when initializing a session with curl_init().
	curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);	//TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
	curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);	//The number of seconds to wait while trying to connect.

	curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);	//The contents of the "User-Agent: " header to be used in a HTTP request.
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);	//To follow any "Location: " header that the server sends as part of the HTTP header.
	curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);	//To automatically set the Referer: field in requests where it follows a Location: redirect.
	curl_setopt($curl, CURLOPT_TIMEOUT, 10);	//The maximum number of seconds to allow cURL functions to execute.
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);	//To stop cURL from verifying the peer's certificate.

	$contents = curl_exec($curl);
	curl_close($curl);
	return $contents;
}

//with google api make sure you have valid access token with exception.

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse =  curl_file_get_contents($url);
$temp = json_decode($xmlresponse,true);
$count = 0; //Setup counter for contacts counting.

foreach($temp['feed']['entry'] as $cnt) {
	$email = $cnt['gd$email']['0']['address']; //As sign email as key here
	if(!empty($email)){
	$c = CheckContact($email); //Check if email exist in database or not 
	if($c<1){ //Email count is less than 1 means this contact is not in database
		$count++; //increment counter
	//	$name = isset($cnt['title']['$t'])? $cnt['title']['$t']:'firstname lastname';
		$nn = $cnt['title']['$t'];
		if(!empty($nn)){
			$name = $nn;
		}else{
			$name = "firstname lastname"; //if there is no name for contact 
		}
		$n = explode(' ',$name);		//split name into first and last name for contact
		$f = $n[0];
		$l = $n[1];
		$firstname = isset($f)?$f:'';
		$lastname = isset($l)?$l:'';
		$phone = isset($cnt['gd$phoneNumber'][0]['$t'])? $cnt['gd$phoneNumber'][0]['$t']:'0000000000'; // Incase phone number is empty and you want to add email into database for this contact
		//echo $firstname." - ".$lastname." - ".$email." - ".$phone."<br/>" ;
		ImportContact($firstname,$lastname,$email,$phone); //Save contact into database.
	}	
	
	
}

}
if($count>1){ 
  //so if saved contacts are more than 1 display success message other wise no contacts are added into database
   ?> 
<div class="alert alert-success"><?php echo $count; ?> contacts imported successfully from gmail.</div>
<?php }else{?>
<div class="alert alert-warning"> No new contact fount for importing.</div>
<?php }?>
&#13;
&#13;
&#13;