【发布时间】:2018-05-23 10:11:02
【问题描述】:
有没有办法使用 Google 联系人 API 检索联系人电话号码 我开发了 PHP 脚本来使用 V2 检索谷歌联系人,它工作正常,但我只收到电子邮件和联系人姓名,我还需要检索电话号码。
下面是我的代码 index.php 文件
session_start();
//包含谷歌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();
回调.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;
}
【问题讨论】:
-
你有没有试过做一个 print_r($contact) 来看看它在哪里。可能会帮助您将其映射到正确的变量。
-
是,但列表中没有电话号码。
-
因为还没有添加
-
所以我需要知道如何添加号码?
标签: php google-api google-api-client