Jul 15, 2022 3:17 PM - edited Jul 15, 2022 3:34 PM
I'm using the hubspot-api-php package
Trying to call the update endpoint
PATCH
/crm/v3/objects/contacts/{contactId}
This is what the hubspot developer docs uses as an example:
<?php
use HubSpot\Factory;
use HubSpot\Client\Crm\Contacts\ApiException;
$client = Factory::createWithApiKey("YOUR_HUBSPOT_API_KEY");
$properties = [
"company" => "Biglytics",
"email" => "bcooper@biglytics.net",
"firstname" => "Bryan",
"lastname" => "Cooper",
"phone" => "(877) 929-0687",
"website" => "biglytics.net"
];
$SimplePublicObjectInput = new SimplePublicObjectInput(['properties' => $properties]);
try {
$apiResponse = $client->crm()->contacts()->basicApi()->update("433380577", $SimplePublicObjectInput);
var_dump($apiResponse);
} catch (ApiException $e) {
echo "Exception when calling basic_api->update: ", $e->getMessage();
}
This is my implementation:
function update_contact( $contact_id, $properties )
{
$client = Factory::createWithAccessToken(get_option('hs_api'));
$SimplePublicObjectInput = new SimplePublicObjectInput(['properties' => $properties]);
try {
$apiResponse = $client->crm()->contacts()->basicApi()->update($contact_id, $SimplePublicObjectInput);
error_log(print_r($apiResponse,true));
} catch (ApiException $e) {
error_log("Exception when calling basic_api->update: ". $e->getMessage());
}
}
Note that $properties looks like this when inspected:
Array
(
[address] => 123 Main St
[address_2] => P.O. 12
[city] => Vancouver
[email] => email@example.com
[firstname] => Kyle
[lastname] =>
[mobilephone] => (877) 929-0687
[province] =>
[zip] =>
)
This is the response:
Exception when calling basic_api->update: [400] Client error: `PATCH https://api.hubapi.com/crm/v3/objects/contacts/[the contact id]` resulted in a `400 Bad Request` response:
{"status":"error","message":"No properties found to update, please provide at least one.","correlationId":"be4a7ab7-2069 (truncated...)
The endpoint with the same properties and contact id works when using Postman, and also when using the "test call" feature in the API documentation.
Am I doing something wrong?
Solved! Go to Solution.
a month ago
Hi SMarquez,
I solved this by forgoing the HubSpot package and using PHP curl functions instead. This is my implementation for updating contacts:
function update_contact( $contact_id, $properties, $subscribed )
{
$params = json_encode(
[
'properties' => $properties
]
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/contacts/" . $contact_id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . get_option('hs_api'),
"accept: application/json",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
}
I used curl functions for all updates, subscribes, and unsubscribe interations, as the package just wasn't working. The package does work when using the search api, however.
Jul 15, 2022 6:33 PM
Hi, @kyleffotf 👋 Thanks for reaching out. Let's see if we can get the conversation started — @himanshurauthan do you have any PHP magic you can share?
Thank you! — Jaycee
Sep 18, 2022 7:48 PM
I have the same error, what is the solution?
a month ago
Hi SMarquez,
I solved this by forgoing the HubSpot package and using PHP curl functions instead. This is my implementation for updating contacts:
function update_contact( $contact_id, $properties, $subscribed )
{
$params = json_encode(
[
'properties' => $properties
]
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/contacts/" . $contact_id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PATCH",
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . get_option('hs_api'),
"accept: application/json",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
}
I used curl functions for all updates, subscribes, and unsubscribe interations, as the package just wasn't working. The package does work when using the search api, however.