Api search contact by email in php - invalid json

VladTit
Member

Hi there,

 

I use this api doc - https://developers.hubspot.com/docs/api/crm/contacts

at the bottom we could see last function for search, where we should be able to get contact by email, but for some reason we always get response Invalid input JSON on line 1

 

Code generated with example params - is not a valid php code - https://i.imgur.com/P6zmRfv.png

 

We cant insert this to project, and when I try to change that to row, or json_encode() params - I always get response about invalid Json

 

Pls advice

 

Thank you,

Vlad

0 Upvotes
5 Replies 5
dennisedson
Community Manager

@VladTit ,

Any chance you could copy the generated json and post it here?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
VladTit
Member

Hi Dennisedson,

 

you could see code in Teun comment

this is exactly that I checked

1) from hubspot documentation

2) tried to refactor it to be correct json file - alway get response that json is not valid

 

Thank you,

Vlad

0 Upvotes
Teun
Guide | Diamond Partner

HI @VladTit ,

I've recreated your code and it generates the following code:

 

<?php
use HubSpot\Factory;
use HubSpot\Client\Crm\Contacts\ApiException;

$client = Factory::createWithApiKey("YOUR_HUBSPOT_API_KEY");

$publicObjectSearchRequest = new PublicObjectSearchRequest(['filter_groups' => [{"filters":[{"value":"test@test.com","propertyName":"email","operator":"EQ"}]}], 'sorts' => ["email"], 'properties' => ["email"], 'limit' => 10, 'after' => 0]);
try {
    $apiResponse = $client->crm()->contacts()->searchApi()->doSearch($publicObjectSearchRequest);
    var_dump($apiResponse);
} catch (ApiException $e) {
    echo "Exception when calling search_api->do_search: ", $e->getMessage();
}

 


If I throw this in Visual Studio code, I get the following error:
Schermafbeelding 2021-09-17 om 21.45.01.png

 

I checked the code, but the JSON is incorrect that is passed in "filter_groups". I am not completely sure, but I think it should be refactored to something like this:

 

<?php

use HubSpot\Factory;
use HubSpot\Client\Crm\Contacts\ApiException;

$client = Factory::createWithApiKey("YOUR_HUBSPOT_API_KEY");
$filters = array(
  'filters' => array(
    array(
      'value' => 'test@test.com',
      'propertyName' => 'email',
      'operator' => 'EQ',
    ),
  ),
);
$publicObjectSearchRequest = new PublicObjectSearchRequest(['filter_groups' => json_encode($filters), 'sorts' => ["email"], 'properties' => ["email"], 'limit' => 10, 'after' => 0]);
try {
  $apiResponse = $client->crm()->contacts()->searchApi()->doSearch($publicObjectSearchRequest);
  var_dump($apiResponse);
} catch (ApiException $e) {
  echo "Exception when calling search_api->do_search: ", $e->getMessage();
}

 

Did my answer solve your issue? Help the community by marking it as the solution.
0 Upvotes
VladTit
Member

Hi there,

 

code from my screen is not my code - thats code form hubspot documentation, and I see same syntax error.

 

And yes, I tried to refactor code to move filter to array and json_encode them, but in this case we always get response from api like this - 

 [400] Client error: `POST https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=xxx` resulted in a `400 Bad Request` response: {"status":"error","message":"Invalid input JSON on line 1, column 17: Cannot deserialize instance of `java.util.HashSet` (truncated...)

 

This is I write here. Code in documentation is not valid, and when I try to refactor it to json I always get error.

any other ideas?

 

Thank you,

Vlad

0 Upvotes
VladTit
Member

Hi there,

 

seems I found soution in this article and using php curl

https://legacydocs.hubspot.com/docs/methods/contacts/get_contact_by_email

 

Thank you,

Vlad