Hello, I have this code in PHP, but it doesn’t always create a contact. I checked apache logs and didn’t find any errors:
public static function addContact(array $userRecord)
{
$accessToken = HUBSPOT_ACCESS_TOKEN;
// The API URL for creating contacts
$url = ‘https://api.hubapi.com/crm/v3/objects/contacts’;
// Data to be sent to the API
$data = [
‘properties’ => [
‘email’ => $userRecord[‘user_email’],
‘firstname’ => $userRecord[‘user_first_name’],
‘lastname’ => $userRecord[‘user_last_name’],
‘phone’ => $userRecord[‘user_phone’]
]
];
// JSON encode the data
$jsonData = json_encode($data);
// Create a stream context for file_get_contents()
$options = [
‘http’ => [
‘method’ => ‘POST’,
‘header’ => “Authorization: Bearer {$accessToken}\r\n” .
“Content-Type: application/json\r\n”,
‘content’ => $jsonData
]
];
$context = stream_context_create($options);
// Make the HTTP request
$response = file_get_contents($url, false, $context);
// Check for errors
if ($response === FALSE) {
error_log("Error creating contact in HubSpot: " . $userRecord[‘user_email’]);
}
}