APIs & Integrations

JNichel
Participant

Problem with API contacts search using php client library

SOLVE

Hi, I'm afraid this is going to be a n00b question but I can figure out what I'm doing wrong.  I would like to pull a list of contacts that have been modified after $date.  Seems easy enough, right?  Going thru the docs I find the search endpoint in the crm section of the v3 API (https://developers.hubspot.com/docs/api/crm/contacts) and copy the code into my IDE.  I make some modifications to hopefully only return contacts modified after 31 January and try running the script.  Unfortunately, there seems to be a syntax error in the code from docs page (I get an error when running it from there as well) and my "skills" are lacking when it comes to finding it.  My code is:

 

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

$client = Factory::createWithApiKey("68153de2-75f0-4aa0-b35b-28bc1a4dd82f");

$PublicObjectSearchRequest = new PublicObjectSearchRequest(['filter_groups' => [{"filters":[{"value":"2022-01-31","propertyName":"lastmodifieddate","operator":"GT"}]}], 'sorts' => ["DESCENDING"], 'properties' => ["ALL"], 'limit' => 20, '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();
}

 

My IDE and server logs are telling me that the error is the curly brace before "filters", ie

 

PublicObjectSearchRequest(['filter_groups' => [{"filters":[{"value"

 

I've tried working with the braces and brackets, but this syntax is foreign to me and I cannot find the right combination.  Any help/point in the right direction is greatly appreciated.

0 Upvotes
1 Accepted solution
tominal
Solution
Guide | Partner
Guide | Partner

Problem with API contacts search using php client library

SOLVE

Hey @JNichel,

 

I would spend some time practicing with PHP arrays. This error is telling you you're trying to mix objects with multidimensional arrays. You are also not allowed to use colons as assignment operators in PHP.

 

In short, replace all of those {} with [] and colons with "=>".

 

RBThomas_0-1644980729272.png

 

P.S. I have found that the PHP SDK is not intuitive. Instead, I wrote my own cURL wrapped functions to handle data with HubSpot.

 

I hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com

View solution in original post

9 Replies 9
tominal
Guide | Partner
Guide | Partner

Problem with API contacts search using php client library

SOLVE

Also, I just noticed this. You are going to need to re-roll your API key since you leaked it in your original post.


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
JNichel
Participant

Problem with API contacts search using php client library

SOLVE

Thank you for this.

0 Upvotes
tominal
Solution
Guide | Partner
Guide | Partner

Problem with API contacts search using php client library

SOLVE

Hey @JNichel,

 

I would spend some time practicing with PHP arrays. This error is telling you you're trying to mix objects with multidimensional arrays. You are also not allowed to use colons as assignment operators in PHP.

 

In short, replace all of those {} with [] and colons with "=>".

 

RBThomas_0-1644980729272.png

 

P.S. I have found that the PHP SDK is not intuitive. Instead, I wrote my own cURL wrapped functions to handle data with HubSpot.

 

I hope that helps!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
JNichel
Participant

Problem with API contacts search using php client library

SOLVE

It did help.  Thank you.  I haven't seriously written any code in a lot of years and even though the example didn't look right to me, I thought it had to be correct because it was in the SDK documentation.  Like maybe it was shorthand for creating arrays.

tominal
Guide | Partner
Guide | Partner

Problem with API contacts search using php client library

SOLVE

That's awesome to hear! Also, I am so used to defining arrays that way I did not even think to mention that it is a shorthand for creating arrays as well.

$var = [];

is the same as

$var = array();

 

I hope you have more luck than I did with the SDK!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
Ismail
Contributor | Elite Partner
Contributor | Elite Partner

Problem with API contacts search using php client library

SOLVE

Hi @JNichel Hubspot uses Unix timestamp in milliseconds for date value

 

So instead if “2022-01-31” use 1643598000000

 

I hope this is helpful.

JNichel
Participant

Problem with API contacts search using php client library

SOLVE

Hi Ismail, that is helpful going forward, but it didn't solve the syntax issue, "syntax error, unexpected '{', expecting ']'", right before filters.

0 Upvotes
Ismail
Contributor | Elite Partner
Contributor | Elite Partner

Problem with API contacts search using php client library

SOLVE

@JNichel I think the property name is "hs_lastmodifieddate"
This works on postman
POST: https://api.hubapi.com/crm/v3/objects/deals/search?hapikey=<yourapikey>

{
"filterGroups": [
{
"filters": [
{
"value": "1643598000000",
"propertyName": "hs_lastmodifieddate",
"operator": "GT"
}
]
}
]
}
JNichel
Participant

Problem with API contacts search using php client library

SOLVE

Ismail, I appreciate your response, but according to the docs here:

 

https://developers.hubspot.com/docs/api/crm/search

 

The property I'm looking to search on for contacts is lastmodifieddate.  The example you're showing is for deals.  I understand that it may work in different areas and/or languages, but I'm trying to use the HubSpot provided PHP SDK (https://developers.hubspot.com/docs/api/overview) so that I can spend more time working with the data instead of writing my own tools.

 

I copied the example from the search endpoint here https://developers.hubspot.com/docs/api/crm/contacts but that is what is throwing the syntax error.  It throws an error when you try to test the example on their own site.  The other parts of the PHP SDK that I've used work fine, so I just wrote something using cURL for the search endpoint until a fix is in place.