【问题标题】:PHP google contact api how to retrive phone numberPHP google联系人api如何检索电话号码
【发布时间】: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


【解决方案1】:

Google Contacts API 允许客户端应用程序查看和更新​​user's contacts。联系人存储在用户的 Google 帐户中;大多数 Google 服务都可以访问联系人列表。

您的客户端应用程序可以使用 Google Contacts API 创建新联系人、编辑或删除现有联系人以及查询符合特定条件的联系人。如果当前经过身份验证的用户尚未添加联系人信息,那么您将无法看到该信息。 Google Contacts api 只能返回它拥有的数据。新的 google people api 也是如此。我还建议您切换到 google people api,它是较新的 api,并且更易于使用。

【讨论】:

  • 号码已经在列表中但我无法检索,可能是我的代码有错误
  • 如果数据不存在,您已认证的用户需要将其添加到contacts.google.com
  • 不知道怎么添加,请告诉我
  • 转到contacts.google.com 找到您想要拥有电话号码的用户。添加电话号码。 contacts.google.com 是一个网络应用程序,如果您在使用它时遇到问题,您可能想尝试在 webapps.stackexchange.com 上提问
猜你喜欢
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 2011-03-23
相关资源
最近更新 更多