如何使用google-api-php-client与People api创建联系人?

时间:2018-09-14 00:50:49

标签: google-api-php-client

有人可以举一个简单的例子来说明如何创建新联系人。我可以授权并获取现有的联系人。但是我已经搜索并搜索了无法正常工作的php示例。我可以在https://developers.google.com/people/v1/write-people#create-a-new-contact找到所有 这是Java代码吗?

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();

我可以找出转换为php的第一行和最后一行。但是我对如何配置设置GivenName和FamilyName的数组感到困惑。

更新:这行得通,但似乎很长。任何建议,将不胜感激!希望这对其他人有帮助。我从stackoverflow专家那里学到了很多东西!

$people_service = new Google_Service_PeopleService($gClient);

    $person = new Google_Service_PeopleService_Person();

    $email1 = new Google_Service_PeopleService_EmailAddress();
    $email1->setValue('test@example.com');
    $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;

2 个答案:

答案 0 :(得分:0)

通过 jdpedrie

可以更轻松地添加人员/联系人
    $people_service = new Google_Service_PeopleService($gClient);
    $person = new Google_Service_PeopleService_Person([
    'names' => [
        [
            'givenName' => 'foobar',
            'familyName' => 'barfoo'
        ]
    ],
    'emailAddresses' => [
        [
            'value' => 'test@example.com'
        ], [
            'value' => 'test2@example.com'
        ]
    ],
    'phoneNumbers' => [
        [
            'value' => '0777677305',
            'type' => 'home'
        ],
        [
            'value' => '0777677305',
            'type' => 'mobile'
        ],
    ]
]);

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

答案 1 :(得分:0)

我可以执行此代码而不会出错,但看不到已创建的联系人

    $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('xx@xx.com');
    $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;

如果运行此示例,我可以看到联系人,您知道为什么吗? (https://developers.google.com/people/quickstart/php#step_3_set_up_the_sample