谷歌人 API |从回复中提取电子邮件地址 | PHP

问题描述

我正在探索 Google People API 以获取经过身份验证的用户联系人的电子邮件。下面的代码非常适合从 Google 获取联系人信息。我也可以使用函数 getdisplayName() 提取显示名称

但是我无法从回复提取电子邮件地址。当我打印来自 Google 的回复时,我可以看到电子邮件地址,但我无法提取它。任何帮助都会有所帮助。

请看下面的代码



//Include Configuration File
include('config.PHP');

//print_r($client);
$login_button = '';

//This $_GET["code"] variable value received after user has login into their Google Account redirct to PHP script then this variable value has been received
if(isset($_GET["code"]))
{
 //It will Attempt to exchange a code for an valid authentication token.
 $token = $client->fetchAccesstokenWithAuthCode($_GET["code"]);

 //This condition will check there is any error occur during geting authentication token. If there is no any error occur then it will execute if block of code/
  if(!isset($token['error']))
  {
    //Set the access token used for requests
    $client->setAccesstoken($token['access_token']);

    //Store "access_token" value in $_SESSION variable for future use.
    $_SESSION['access_token'] = $token['access_token'];

    // GET CONTACTS USING token

    // $client=getClient();
    $service = new Google_Service_PeopleService($client);

    $optParams = array('personFields' => 'names,emailAddresses');
    $results = $service->people_connections->listPeopleConnections('people/me',$optParams);
    // print_r($results);
    if (count($results->getConnections()) == 0)
    {
      print "No connections found.\n";
    }else
    {
      print "People:\n";
      $i=1;
      foreach ($results->getConnections() as $person)
      {
        if (count($person->getNames()) == 0)
        {
          print "No names found for this connection\n";
        }else
        {
          $names = $person->getNames();
          $name = $names[0];
          echo $i;
          printf("%s\n",$name->getdisplayName());

          $email_ids = $person->getemailAddresses(); 
          $email_id = $email_ids[0];
          printf("%s\n",$email_id->getValue()); //THIS IS WHERE THE ERROR HAPPENS AND I'M UNABLE TO 
 //DETERmine THE RIGHT FUNCTION

        }
        echo "<br><br>";
        $i++;
      }
    }
  }
}

//This is for check user has login into system by using Google account,if User not login into system then it will execute if block of code and make code for display Login link for Login using Google account.

 //Create a URL to obtain user authorization
 $login_button = '<a href="'.$client->createAuthUrl().'"><img src="https://i.stack.imgur.com/mGHPI.png" /></a>';

?>
<html>
 <head>
  <Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>PHP Login using Google Account</title>
  <Meta content='width=device-width,initial-scale=1,maximum-scale=1' name='viewport'/>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  
 </head>
 <body>
  <div class="container">
   <br />
   <h2 align="center">PHP Login using Google Account</h2>
   <br />
  </div>
 </body>
</html>

代码输出

Google_Service_PeopleService_UserDefined [userDefinedDataType:protected] => 
array [internal_gapi_mappings:protected] => Array ( ) 
[modelData:protected] => Array ( ) 
[processed:protected] => Array ( ) 
[names] => Array 
( [0] => Google_Service_PeopleService_Name Object 
( [displayName] => AbC
[displayNameLastFirst] => ABC 
[familyName] => 
[givenname] => ABC
[honorificPrefix] => 
[honorificSuffix] => 
[MetadataType:protected] => 
Google_Service_PeopleService_FieldMetadata 
[MetadataDataType:protected] => 
[middleName] => 
[phoneticFamilyName] => 
[phoneticFullName] => 
[phoneticGivenname] => 
[phoneticHonorificPrefix] => 
[phoneticHonorificSuffix] => 
[phoneticMiddleName] => 
[unstructuredname] => Abc 
[internal_gapi_mappings:protected] => Array ( ) 
[modelData:protected] => Array ( ) 
[processed:protected] => Array ( ) 
[Metadata] => 
Google_Service_PeopleService_FieldMetadata Object 
( [primary] => 1 
[sourceType:protected] => 
Google_Service_PeopleService_Source [sourceDataType:protected] => 
[verified] => 
[internal_gapi_mappings:protected] => Array ( ) 
[modelData:protected] => Array ( ) 
[processed:protected] => Array ( ) 
[source] => Google_Service_PeopleService_Source Object 
( [etag] => [id] => 7cc959850c2ef692 [profileMetadataType:protected] => Google_Service_PeopleService_ProfileMetadata [profileMetadataDataType:protected] => 
[type] => 
CONTACT [updateTime] => 
[internal_gapi_mappings:protected] => Array ( ) 
[modelData:protected] => Array ( ) 
[processed:protected] => Array ( ) ) ) ) ) 
[emailAddresses] => Array ( [0] => 
Google_Service_PeopleService_EmailAddress Object 
( [displayName] => [formattedType] => 
[MetadataType:protected] => 
Google_Service_PeopleService_FieldMetadata 
[MetadataDataType:protected] => [type] => 
[value] => abc@gmail.com 
[internal_gapi_mappings:protected] => 
Array ( ) [modelData:protected] => Array ( ) 
[processed:protected] => Array ( ) 
[Metadata] => Google_Service_PeopleService_FieldMetadata Object 
( [primary] => 1 [sourceType:protected] => ```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)