【问题标题】:People API searchContacts using google people service使用谷歌人员服务的人员 API 搜索联系人
【发布时间】:2021-03-30 05:41:06
【问题描述】:

我尝试使用带有 PHP 客户端人员服务的 Google People API 添加新联系人,并且效果很好。我使用了以下代码,

$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$name = new Google_Service_PeopleService_Name();
$name->setGivenName($_name);
$person->setNames($name);
$phone1 = new Google_Service_PeopleService_PhoneNumber();   
$phone1->setValue($_phone);
$phone1->setType('home');
$person->setPhoneNumbers($phone1);
$exe = $service->people->createContact($person)->execute;

现在我想检查特定电子邮件是否已经存在,因此尝试了 API 的 searchContacts 方法。它在 API 资源管理器中运行良好。 参考: https://developers.google.com/people/api/rest/v1/people/searchContacts

但是当尝试使用以下代码时,

$service = new Google_Service_PeopleService($client);
$optParams = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'emailAddresses', 'key'=>'XXXX');
try{
    $rslt = $service->people->searchContacts($optParams);
} catch(Exception $ex) {
    echo $e->getMessage();  
}

我收到以下错误,

PHP 致命错误:调用未定义的方法 Google_Service_PeopleService_Resource_People::searchContacts()

查看错误我确定调用该方法的方式是错误的。我已经搜索但无法获得正确的文档来指导该方向。 任何帮助将不胜感激。

谢谢

【问题讨论】:

  • key 来自哪里?似乎不是方法 searchContacts 的有效参数。
  • 如果我删除了密钥,它没有任何区别。我只是尝试了各种组合(在 API Explorer HTTP json 中它是参数之一)。无论如何,错误显示“PHP 致命错误:调用未定义的方法 Google_Service_PeopleService_Resource_People::searchContacts()”所以这是方法问题而不是参数问题。

标签: google-people-api


【解决方案1】:

看起来我们不能使用人员服务对象调用 searchContacts,所以我使用 curl json 来让它工作。如果有人需要,我已经使用了以下代码。

$url_parameters = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'names,phoneNumbers,emailAddresses,organizations', 'key'=>'XXXXX');

$url_calendars = 'https://people.googleapis.com/v1/people:searchContacts?'. http_build_query($url_parameters);

$ch = curl_init();      
curl_setopt($ch, CURLOPT_URL, $url_calendars);      
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
$data = json_decode(curl_exec($ch), true); 
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      
if($http_code != 200) 
    throw new Exception('Error : Failed to search contact');

return $data;

谢谢

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多