API to create a contact doesn't always work

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’]);

}

}

Your PHP code attempts to add a contact to HubSpot CRM using its API, but it’s not always successful, despite no errors being logged in the Apache logs. Several factors could contribute to this, including API rate limits, validation errors, network issues, HubSpot configuration, and error handling. To improve reliability, ensure your code addresses these potential issues effectively.

Hi,
Thank you for your quick response.
Would you provide me with the php code or sample app, please. For some reason, you only have the javascript code on the site.