【问题标题】:How to create Contact with people api using google-api-php-client?如何使用 google-api-php-client 创建联系人 api?
【发布时间】:2018-09-14 00:50:49
【问题描述】:

有人可以举一个简单的例子来说明如何创建新联系人。我可以授权并获取现有的联系人。但我已经搜索并搜索了不起作用的尝试 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 的第一行和最后一行。但我对如何配置 Array 设置 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;

【问题讨论】:

    标签: google-api-php-client


    【解决方案1】:

    感谢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);
    

    【讨论】:

      【解决方案2】:

      我可以毫无错误地执行此代码,但我没有看到创建的联系人

          $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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多