APIs & Integrations

mgreenaw
Participant

PHP API contacts search not applying filter condition

SOLVE
$properties = [
    'email',
    'firstname',
    'lastname',
];

$filter = new Filter([
    'value' => $lastuser->email,
    'propertyName' => 'email',
    'operator' => 'EQ'
]);

$filterGroup = new FilterGroup([
    'filters' => [$filter]
]);

$searchRequest = new PublicObjectSearchRequest([
    'filterGroups' => [$filterGroup],
    'properties' => $properties,
    'limit' => 1
]);

try {
    $searchResults = $client->crm()->contacts()->searchApi()->doSearch($searchRequest);
} catch(ApiException $e) {
    // ...
}

Hi,

 

I am having difficulty getting the contacts search by parameter to work in the PHP API.  The attached is the code that I have arrived at after much back and forth with support and the AI chat which should, theoretically, return me the contact with the email address matching $lastuser->email (which is a clean string with no extra spaces etc.) 

 

What I get, instead, is all contacts in ID order from 1 upwards, limited only by the "limit" value in the $searchRequest.

 

I'm either doing something wrong, which is of course entirely possible, or there's a problem with the PHP API search. I can make a standard Postman call to https://api.hubapi.com/crm/v3/objects/contacts/search work without issue.

 

Any help greatly appreciated and if any more information is required I'm happy to provide it within data protection reasons of course. 

 

Kind regards,

Martin

0 Upvotes
1 Accepted solution
MichaelMa
Solution
Contributor

PHP API contacts search not applying filter condition

SOLVE

First, have you tried retrieving the payload for what is being sent to Hubspot? It would give you an idea if there is an issue with your code and the payload.

 

Second, have you looked at the PHP SDK github README? It has a pretty clear example of how to do a search filter for contacts.

 

https://github.com/HubSpot/hubspot-api-php

"Search for a contact"

$filter = new \HubSpot\Client\Crm\Contacts\Model\Filter();
$filter
    ->setOperator('EQ')
    ->setPropertyName('email')
    ->setValue($search);

$filterGroup = new \HubSpot\Client\Crm\Contacts\Model\FilterGroup();
$filterGroup->setFilters([$filter]);

$searchRequest = new \HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest();
$searchRequest->setFilterGroups([$filterGroup]);

// Get specific properties
$searchRequest->setProperties(['firstname', 'lastname', 'date_of_birth', 'email']);

// @VAR CollectionResponseWithTotalSimplePublicObject $contactsPage
$contactsPage = $hubspot->crm()->contacts()->searchApi()->doSearch($searchRequest);

 

It looks like you're not using the functions to set the data but attempting to manually assign the data to what you expect to be the payload fields (which seems like it doesn't work?).

 

 

View solution in original post

4 Replies 4
MichaelMa
Solution
Contributor

PHP API contacts search not applying filter condition

SOLVE

First, have you tried retrieving the payload for what is being sent to Hubspot? It would give you an idea if there is an issue with your code and the payload.

 

Second, have you looked at the PHP SDK github README? It has a pretty clear example of how to do a search filter for contacts.

 

https://github.com/HubSpot/hubspot-api-php

"Search for a contact"

$filter = new \HubSpot\Client\Crm\Contacts\Model\Filter();
$filter
    ->setOperator('EQ')
    ->setPropertyName('email')
    ->setValue($search);

$filterGroup = new \HubSpot\Client\Crm\Contacts\Model\FilterGroup();
$filterGroup->setFilters([$filter]);

$searchRequest = new \HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest();
$searchRequest->setFilterGroups([$filterGroup]);

// Get specific properties
$searchRequest->setProperties(['firstname', 'lastname', 'date_of_birth', 'email']);

// @VAR CollectionResponseWithTotalSimplePublicObject $contactsPage
$contactsPage = $hubspot->crm()->contacts()->searchApi()->doSearch($searchRequest);

 

It looks like you're not using the functions to set the data but attempting to manually assign the data to what you expect to be the payload fields (which seems like it doesn't work?).

 

 

mgreenaw
Participant

PHP API contacts search not applying filter condition

SOLVE

This is entirely plausible, the code I quoted above was code that had been arrived at after a lot of back and forth with various support channels. I'm pretty sure I started out with the code from the readme but this is where I've ended up!  I will give the above a go with the assignment functions again and see what that produces.

 

When you say "have you tried retrieving the payload" - is there a function for that?

0 Upvotes
MichaelMa
Contributor

PHP API contacts search not applying filter condition

SOLVE

I haven't used the PHP SDK but assuming you can probably put var_dump($searchRequest); right before the request to see what data is being sent.

 

It looks like the SDK encodes that object into JSON to create the payload so you might need to do that also to get a clearer view of the payload.

https://github.com/HubSpot/hubspot-api-php/blob/6659a2f85b5d2a01c890a5e5ce840c09e220c5e3/codegen/Crm...  

https://github.com/HubSpot/hubspot-api-php/blob/6659a2f85b5d2a01c890a5e5ce840c09e220c5e3/codegen/Crm...  

 

An alternative, is to make the request FAIL (400 error) and then look at the error log in Hubspot for the API. 400 errors in the API gives you the payload and error response while 200 (success) requests do not. 

 

mgreenaw
Participant

PHP API contacts search not applying filter condition

SOLVE

Ah yep am already var_dumping. The bit on the 400 error logging is really useful, thank you.  FWIW the use of the functions works fine, so is deeply disappointing that about the only thing that the AI chat and the human support people could agree on (the way to build the request) was the source of the problem all along. Anyway, you've helped unblock my process so many thanks for that!

0 Upvotes