Google-api-php-client: How to Create new Contact using People API with multiple values

Created on 12 Jun 2019  路  6Comments  路  Source: googleapis/google-api-php-client

these codes are working but i cannot push multiple phone numbers and email addresses

    $person = new Google_Service_PeopleService_Person();


    $name = new Google_Service_PeopleService_Name();
    $name->setGivenName('00nirman');
    $name->setFamilyName('weerasinghe');
    $person->setNames($name);

$email1 = new Google_Service_PeopleService_EmailAddress();
    $email1->setValue('[email protected]');
    $person->setEmailAddresses($email1);

$phone1 = new Google_Service_PeopleService_PhoneNumber();   
    $phone1->setValue('0777677305');
    $phone1->setType('home');
    $person->setPhoneNumbers($phone1);
   $exe = $people_service->people->createContact($person)->execute;

Is there any way to do that

question

Most helpful comment

$person = new Google_Service_PeopleService_Person([
    'names' => [
        [
            'givenName' => 'foobar',
            'familyName' => 'barfoo'
        ]
    ],
    'emailAddresses' => [
        [
            'value' => '[email protected]'
        ], [
            'value' => '[email protected]'
        ]
    ],
    'phoneNumbers' => [
        [
            'value' => '0777677305',
            'type' => 'home'
        ],
        [
            'value' => '0777677305',
            'type' => 'mobile'
        ],
    ]
]);

$exe = $service->people->createContact($person);

All 6 comments

Provide the email addresses and phone numbers as arrays of objects:

$email1 = new Google_Service_PeopleService_EmailAddress();
$email1->setValue('[email protected]');

$email2 = new Google_Service_PeopleService_EmailAddress();
$email2->setValue('[email protected]');

$person->setEmailAddresses([$email1, $email2]);

Provide the email addresses and phone numbers as arrays of objects:

$email1 = new Google_Service_PeopleService_EmailAddress();
$email1->setValue('[email protected]');

$email2 = new Google_Service_PeopleService_EmailAddress();
$email2->setValue('[email protected]');

$person->setEmailAddresses([$email1, $email2]);

than you
this is working im using this one for now
but this is creating objects for every email i'm looking other options to use easily
example calendar Api creating events

Please explain in more depth what you are looking for. I'm not sure what you mean.

Please explain in more depth what you are looking for. I'm not sure what you mean.

Instead of Using This

$email1 = new Google_Service_PeopleService_EmailAddress();
$email1->setValue('[email protected]');

$email2 = new Google_Service_PeopleService_EmailAddress();
$email2->setValue('[email protected]');

similer format like this


$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer products.',
  'start' => array(
    'dateTime' => '2019-05-27T09:00:00',
    'timeZone' => 'Asia/Colombo',
  ),
  'end' => array(
    'dateTime' => '2019-05-27T17:00:00',
    'timeZone' => 'Asia/Colombo',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=1'
  ),
  'attendees' => array(
    array('email' => '[email protected]'),
    array('email' => '[email protected]'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$person = new Google_Service_PeopleService_Person([
    'names' => [
        [
            'givenName' => 'foobar',
            'familyName' => 'barfoo'
        ]
    ],
    'emailAddresses' => [
        [
            'value' => '[email protected]'
        ], [
            'value' => '[email protected]'
        ]
    ],
    'phoneNumbers' => [
        [
            'value' => '0777677305',
            'type' => 'home'
        ],
        [
            'value' => '0777677305',
            'type' => 'mobile'
        ],
    ]
]);

$exe = $service->people->createContact($person);
$person = new Google_Service_PeopleService_Person([
    'names' => [
        [
            'givenName' => 'foobar',
            'familyName' => 'barfoo'
        ]
    ],
    'emailAddresses' => [
        [
            'value' => '[email protected]'
        ], [
            'value' => '[email protected]'
        ]
    ],
    'phoneNumbers' => [
        [
            'value' => '0777677305',
            'type' => 'home'
        ],
        [
            'value' => '0777677305',
            'type' => 'mobile'
        ],
    ]
]);

$exe = $service->people->createContact($person);

You are a lifesaver thank you very much this code are working

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unixkapl picture unixkapl  路  3Comments

ysaurabh33 picture ysaurabh33  路  3Comments

kungufli picture kungufli  路  3Comments

bencromwell picture bencromwell  路  3Comments

Romain picture Romain  路  4Comments