Creating a php webhook that submits form data into HubSpot CRM

Hi All,

I am creating a php webhook to collect the data from an HTML form and submit them into HubSpot CRM.

I have already created a private app for that.

Can anyone tell me what I am missing:

<?php
if ($_SERVER[‘REQUEST_METHOD’] === ‘POST’) {
// Replace with your HubSpot Private App credentials
$clientId = ‘here_I_added_the_PrivateApp_AccessToken’;
$clientSecret = ‘here_I_added_the_PrivateApp_ClientSecret’;
$hubspotApiBaseUrl = ‘https://api.hubapi.com’;

// form data
$formData = $_POST;

// HubSpot CRM API endpoint for creating a contact
$endpoint = “/crm/v3/objects/contacts”;

// HubSpot CRM API URL
$url = $hubspotApiBaseUrl . $endpoint;

// HubSpot CRM API request headers
$headers = [
‘Content-Type: application/json’,
];

// HubSpot CRM API request data
$data = [
‘properties’ => [

[‘property’ => ‘firstname’,‘value’ => $formData[‘Full-Name’],],
[‘property’ => ‘company’,‘value’ => $formData[‘Company-Name’],],
[‘property’ => ‘email’,‘value’ => $formData[‘email’],]

]
];

// Initialize cURL session
$ch = curl_init($url);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ‘POST’);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute cURL session
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ’ . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Log response (for debugging)
file_put_contents(‘hubspot_response.log’, $response . PHP_EOL, FILE_APPEND | LOCK_EX);
} else {
http_response_code(405); // Method Not Allowed
echo ‘Method Not Allowed’;
}
?>

========END PHP Code=========================

And I am getting this error in the hubspot_response.log file:
{“status”:“error”,“message”:“Authentication credentials not found. This API supports OAuth 2.0 authentication and you can find more details at Accounts Dashboard | HubSpot”}

@geohajj - interesting project and I’m sure that such and approach would give you a lot of fexibility in form design not easily accessible in HubSpot native forms.

How does the form data get from your client side environment to the server environment necessary to execute the HubSpot API call? Remember that web client side API calls are not permitted due to potential exposure of token credentials amongst other things -- Only HubDB calls are exempt from this last time I looked.

This may be a facet of how you have written up the problem in this post, but the header for the API call will for sure need to includeth extra data of the private app token as documented - could the issue be as simple as that?

Hope that I don’t have the wrong end of the stick here and that this is helpful.

Steve