Carol Lillefield
Carol Lillefield

Reputation: 31

How to create Contact with people api using google-api-php-client?

Could someone give a simple example of how to create a new contact. I can authorize and get the existing contacts. But I have searched and searched tried php examples that don't work. All I can find at https://developers.google.com/people/v1/write-people#create-a-new-contact is this Java Code:

Person contactToCreate = new Person();
List names = new ArrayList<>();
names.add(new Name().setGivenName("John").setFamilyName("Doe"));
contactToCreate.setNames(names);

Person createdContact = peopleService.people().createContact(contactToCreate).execute();

I can figure out the first and last line to convert to php. But I am confused as to how to configure the Array setting the GivenName and FamilyName.

UPDATE: This works, but it seems like the long way. Any suggestions would be appreciated! Hope this helps someone else. I learn so much from stackoverflow experts!

$people_service = new Google_Service_PeopleService($gClient);

    $person = new Google_Service_PeopleService_Person();

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

    $name = new Google_Service_PeopleService_Name();
    $name->setGivenName('firstName');
    $name->setFamilyName('lastName');

    $person->setNames($name);

    $exe = $people_service->people->createContact($person)->execute;

Upvotes: 2

Views: 2158

Answers (2)

Luciano Dicono
Luciano Dicono

Reputation: 1

I can execute this code without errors but i don't see the created Contact

    $client = new \Google_Client();
    $client->setAuthConfig('/Users/..../xxx.json');

    $client->setScopes(['https://www.googleapis.com/auth/contacts']);
 
    $people_service = new \Google_Service_PeopleService($client);
    
    $person = new \Google_Service_PeopleService_Person();

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

    $name = new \Google_Service_PeopleService_Name();
    $name->setGivenName('haruki');
    $name->setFamilyName('murakami');
    $person->setNames($name);

    $phone1 = new \Google_Service_PeopleService_PhoneNumber();
    $phone1->setValue('5491141653254');
    $phone1->setType('mobile');
    $person->setPhoneNumbers($phone1);        

    $people_service->people->createContact($person)->execute;

If i run this example i can see the contact, do you know why? (https://developers.google.com/people/quickstart/php#step_3_set_up_the_sample)

Upvotes: 0

Abhishek kandari
Abhishek kandari

Reputation: 196

Shorter way to add people/contact, thanks to jdpedrie

    $people_service = new Google_Service_PeopleService($gClient);
    $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);

Upvotes: 3

Related Questions