Search code examples
google-people-api

People API searchContacts using google people service


I have tried adding a new contact using Google People API with PHP client people service and it worked fine. I have used following code,

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

Now I want to check if particular email already exists so tried searchContacts method of API. It works fine in API explorer. ref: https://developers.google.com/people/api/rest/v1/people/searchContacts

But when have tried with following code,

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

i am getting following error,

PHP Fatal error: Call to undefined method Google_Service_PeopleService_Resource_People::searchContacts()

Looking at the error i am sure that the way to call the method is wrong. I have searched but not able to get proper documentation in guiding in that direction. Any help will be appreciated.

Thanks


Solution

  • Looks like we cannot call searchContacts using people service object so i have used curl json to get it worked. I have used following code if anyone needs.

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

    Thanks