Introduction:
HubSpot is a powerful CRM platform that allows businesses to manage their contacts efficiently. In this tutorial, we will create a simple custom contact form using PHP that integrates with HubSpot’s API to store user data directly into your HubSpot CRM.
Why Use a Custom Contact Form?
A custom contact form offers more flexibility than standard HubSpot forms. You can customize the design, add validations, and integrate with other tools as needed.
Steps to Create a Custom HubSpot Contact Form
1. Get Your HubSpot API Key
To connect to HubSpot's API, you need an API key. You can find it by navigating to:
- HubSpot Dashboard → Settings → Integrations → API Key
Copy and keep this key safe as we will use it in our code.
2. Create the PHP Form
Below is the PHP code for the custom HubSpot contact form:
// Define HubSpot API Key $hubspot_api_key = 'YOUR_HUBSPOT_API_KEY'; // Check if form is submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { $contact_data = [ 'fields' => [ ['name' => 'firstname', 'value' => $_POST['firstname']], ['name' => 'lastname', 'value' => $_POST['lastname']], ['name' => 'email', 'value' => $_POST['email']] ] ]; $endpoint = "https://api.hubapi.com/crm/v3/objects/contacts?hapikey=$hubspot_api_key"; $ch = curl_init($endpoint); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($contact_data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); $response = curl_exec($ch); curl_close($ch); echo "Contact submitted successfully!"; }
3. Create the HTML Form
Add this HTML code to your project to create a user-friendly form:
HubSpot Contact Form HubSpot Contact Form
4. Deploy and Test the Form
- Save the PHP file and host it on your web server.
- Open the form in your browser, enter details, and submit.
- The submitted contact details should now appear in your HubSpot CRM.