![](/img/trans.png)
[英]Determining Address type or phone number type with Google Contact API
[英]PHP google contact api how to retrive phone number
有沒有一種使用Google聯系人API檢索聯系人電話號碼的方法,我開發了PHP腳本來使用V2檢索Google聯系人,它工作正常,但是我只收到電子郵件和聯系人姓名,我還需要檢索電話號碼。
下面是我的代碼index.php文件
session_start();
//包含Google api庫
require_once '/usr/share/nginx/html/dev/gcontact/vendor/autoload.php';// or wherever autoload.php is located
$google_client_id = 'google_client_id';
$google_client_secret = 'google_client_secret';
$google_redirect_uri = 'http://localhost/dev/gcontact/callback.php';
$client = new Google_Client();
$client -> setApplicationName('My application name');
$client -> setClientid($google_client_id);
$client -> setClientSecret($google_client_secret);
$client -> setRedirectUri($google_redirect_uri);
$client -> setAccessType('online');
$client -> setScopes(array('https://www.googleapis.com/auth/contacts','https://www.google.com/m8/feeds','https://www.google.com/m8/feeds/user','https://www.googleapis.com/auth/userinfo.email'));
echo $googleImportUrl = $client -> createAuthUrl();
Callback.php
require_once '/usr/share/nginx/html/dev/gcontact/vendor/autoload.php';// or wherever autoload.php is located
if (isset($_GET['code'])) {
$auth_code = $_GET["code"];
$_SESSION['google_code'] = $auth_code;
}
$google_client_id = 'google_client_id';
$google_client_secret = 'google_client_secret';
$google_redirect_uri = 'http://localhost/dev/gcontact/callback.php';
if(isset($_SESSION['google_code'])) {
$auth_code = $_SESSION['google_code'];
$max_results = 500;
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($google_client_id),
'client_secret'=> urlencode($google_client_secret),
'redirect_uri'=> urlencode($google_redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value)
{
$post .= $key.'='.$value.'&';
}
$post = rtrim($post,'&');
$result = curl('https://accounts.google.com/o/oauth2/token',$post);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$client = new Google_Client();
$client->setAccessToken($accesstoken);
$google_oauthV2 = new Google_Service_Oauth2($client);
$gpUserProfile = $google_oauthV2->userinfo->get();
$browser = $_SERVER['HTTP_USER_AGENT'];
$user_time = date('D M d Y H:i:s O');
$xmlresponse = curl($url);
$contacts = json_decode($xmlresponse,true);
$return = array();
if (!empty($contacts['feed']['entry'])) {
foreach($contacts['feed']['entry'] as $contact) {
$num = isset($cnt['gd$phoneNumber'][0]['$t'])? $cnt['gd$phoneNumber'][0]['$t']:'0000000000';
$num = $cnt['gd$phoneNumber'][0]['$t'];
//retrieve Name and email address
$return[] = array (
'name'=> $contact['title']['$t'],
'email' => $contact['gd$email'][0]['address'],
'phone' => $num // this always gives 0000000000
);
}
}
$google_contacts = $return;
//echo count($google_contacts);
echo "<pre>";
print_r($google_contacts); exit;
makeRequest($gpUserProfile['name'],$gpUserProfile['email'],$user_time,$browser);
unset($_SESSION['google_code']);
}
function curl($url, $post = "") {
$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.
if ($post != "") {
curl_setopt($curl, CURLOPT_POST, 5);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
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;
}
Google Contacts API允許客戶端應用程序查看和更新用戶的聯系人 。 聯系人存儲在用戶的Google帳戶中; 大多數Google服務都可以訪問聯系人列表。
您的客戶端應用程序可以使用Google Contacts API創建新的聯系人,編輯或刪除現有的聯系人,以及查詢符合特定條件的聯系人。 如果當前經過身份驗證的用戶尚未添加聯系人的信息,則您將無法看到該信息。 Google通訊錄api只能返回其擁有的數據。 新的Google People API也是一樣。 我還建議您切換到Google People API,它是較新的API,並且更易於使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.