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.
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.
$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?).
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.
$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?).
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?
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.
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!