I'm exploring the Google People API to get emails of contacts of authenticated users. The code below works perfectly fine for getting the contacts information from Google. I'm able to extract the display names as well by using the function getDisplayName().
However I'm unable to extract the emails address from the response. When I print the response from Google I can see the email addresses but I'm unable to extract it. Any assistance will be helpful.
Pease see the code below:
//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>
The output of the code is
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] => ```