简体   繁体   English

PHP Google Contact API如何检索电话号码

[英]PHP google contact api how to retrive phone number

Is there a way to retrieve contact phone number with Google contact API I developed PHP script to retrieve google contact using V2 and it works fine but I am getting only emails and name of the contact, I also need to retrieve a phone number. 有没有一种使用Google联系人API检索联系人电话号码的方法,我开发了PHP脚本来使用V2检索Google联系人,它工作正常,但是我只收到电子邮件和联系人姓名,我还需要检索电话号码。

Below is my code index.php file 下面是我的代码index.php文件

  session_start();

//include google api library //包含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 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;
            }      

The Google Contacts API allows client applications to view and update a user's contacts . Google Contacts API允许客户端应用程序查看和更新用户的联系人 Contacts are stored in the user's Google Account; 联系人存储在用户的Google帐户中; most Google services have access to the contact list. 大多数Google服务都可以访问联系人列表。

Your client application can use the Google Contacts API to create new contacts, edit or delete existing contacts, and query for contacts that match particular criteria. 您的客户端应用程序可以使用Google Contacts API创建新的联系人,编辑或删除现有的联系人,以及查询符合特定条件的联系人。 If the current authenticated user has not added the information for the contact then you are not going to be able to see the information. 如果当前经过身份验证的用户尚未添加联系人的信息,则您将无法看到该信息。 Google contacts api can only return the data that it has. Google通讯录api只能返回其拥有的数据。 This is the same for the new google people api. 新的Google People API也是一样。 I would also recommend that you switch to the google people api its the newer api and much easier to use. 我还建议您切换到Google People API,它是较新的API,并且更易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM