Home » Php » php – How to get data form Google_Service_PeopleService?

php – How to get data form Google_Service_PeopleService?

Posted by: admin July 12, 2020 Leave a comment


I’m currently working with Google_Client api and want to fetch User Name, Phone, Email and User address.

I set-up these scopes:


And when I click on the login with google it asks the correct permissions, and then I fetch the access token with the code provided by Google.

After getting the token I request for people_service and profile data like this:

$token = $this->client->fetchAccessTokenWithAuthCode($_GET['code']);
$people_service = new \Google_Service_PeopleService($this->client);
$profile = $people_service->people->get(
             array('personFields' => 'addresses,birthdays,emailAddresses,phoneNumbers')

It returns a Google_Service_PeopleService_Person object.

But when I try to use method on it like getPhoneNumbers() it returns a Call to undefined method Google_Service_PeopleService_Person::getNames() error.

What is the problem and what can I do?

How to&Answers:

You do not show how exactly are you setting the scope, and the error might be related to that.

Doing this, I get the correct results:

$scopes = [

$client = new Google_Client();
$client->setApplicationName('People API PHP Quickstart');
$client->setPrompt('select_account consent');

 // set the scope

/*  ... actual authentication.   */

$service = new Google_Service_PeopleService( $client );

$optParams = [
    'personFields' => 'names,emailAddresses,addresses,phoneNumbers',

$me = $service->people->get( 'people/me', $optParams );

print_r( $me->getNames() );
print_r( $me->getEmailAddresses() );
print_r( $me->getBirthdays() );
print_r( $me->getPhoneNumbers() );