HubSpot Form Integration with Magento Checkout Page Not Working

I’m having trouble getting my HubSpot form integration to work properly on my Magento checkout confirmation page. The form data is being collected correctly in my $form_data variable, but when I try to send it to HubSpot using cURL, the $result variable comes back empty.

It looks like the cURL request isn’t executing at all. I’m not sure if there’s something special I need to do to make the cURL call work when the page loads. Any ideas what might be going wrong?

<?php
// Send customer data to HubSpot after successful order

$hubspot_token = $_COOKIE['hubspotutk'];  // get tracking cookie
$customer_ip = $_SERVER['REMOTE_ADDR'];  // get user IP
$context_data = array(
    'hutk' => $hubspot_token,
    'ipAddress' => $customer_ip,
    'pageUrl' => 'https://www.mystore.com/checkout/success/',
    'pageTitle' => 'Order Complete - MyStore'
);
$context_json = json_encode($context_data);

// Build form data string
$form_data = "first_name=" . urlencode($customer_firstname)
    . "&last_name=" . urlencode($customer_lastname)
    . "&email_address=" . urlencode($customer_email)
    . "&phone_number=" . urlencode($customer_phone)
    . "&street_address=" . urlencode($billing_address)
    . "&city_name=" . urlencode($billing_city)
    . "&state_province=" . urlencode($billing_state)
    . "&country_code=" . urlencode($billing_country)
    . "&hs_context=" . urlencode($context_json);

// HubSpot form endpoint
$hubspot_url = 'https://forms.hubspot.com/uploads/form/v2/PORTAL-ID/FORM-GUID';

$curl_handle = @curl_init();
@curl_setopt($curl_handle, CURLOPT_POST, true);
@curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $form_data);
@curl_setopt($curl_handle, CURLOPT_URL, $hubspot_url);
@curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
@curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$result = @curl_exec($curl_handle);
@curl_close($curl_handle);
echo $result;

?>

Your cURL request is probably running during page load, which messes with Magento’s response handling. Move the HubSpot submission to run async after the page renders, or trigger it with AJAX from the frontend instead.

I’ve hit this same issue - cURL works perfectly in isolation but breaks when you embed it in checkout success pages. Also check if your server blocks outbound connections on port 443. Some hosts block external API calls from checkout pages for security.

Try logging the curl_error() output temporarily so you can see the actual error instead of just checking for empty results.

I’ve hit this exact problem before - it’s probably a timeout issue. Magento checkout pages have execution limits that kill long API calls. Add curl_setopt($curl_handle, CURLOPT_TIMEOUT, 10); to set a proper timeout window. Check your server error logs too since those @ symbols hide curl errors that’d show you what’s actually breaking. HubSpot forms are picky about field names - double-check your form fields match exactly what you’re sending, especially the underscore formatting.

The Problem:

Your HubSpot form integration in your Magento checkout confirmation page is failing to send data to HubSpot using cURL. The cURL request appears not to be executing, resulting in an empty $result variable. Your code correctly collects form data in the $form_data variable, but the issue lies in the cURL execution itself.

:thinking: Understanding the “Why” (The Root Cause):

The problem is likely due to an incorrect CURLOPT_HTTPHEADER setting in your cURL request. You’re specifying 'application/x-www-form-urlencoded' as the content type, but only the content type itself is passed as a header, not the complete Content-Type header. The @ symbols before your cURL functions are suppressing error messages, making it difficult to diagnose the problem. Furthermore, unspecified timeouts can lead to errors.

:gear: Step-by-Step Guide:

  1. Correct the CURLOPT_HTTPHEADER: Modify your cURL options to correctly set the Content-Type header:
@curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

This ensures that the header is correctly formatted and sent to HubSpot. The previous version was missing the Content-Type: prefix.

  1. Remove Error Suppression: Remove the @ symbols from your cURL functions. These symbols suppress error reporting, making debugging extremely difficult. By removing them, any cURL errors will be reported, allowing you to identify the root cause of the problem:
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($curl_handle, CURLOPT_URL, $hubspot_url);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl_handle);
curl_close($curl_handle);

Now, if there is an error, curl_error($curl_handle) will provide a descriptive message. Log this error to aid in debugging.

  1. Set a Timeout: Add a timeout to your cURL request using CURLOPT_TIMEOUT. This prevents indefinite hanging if the request fails or the HubSpot server is unresponsive. Choose a suitable timeout value, e.g., 10 seconds:
curl_setopt($curl_handle, CURLOPT_TIMEOUT, 10); 
  1. Verify HubSpot Form Field Names: Double-check that the field names in your $form_data string exactly match the field names defined in your HubSpot form. Even minor discrepancies (like inconsistent use of underscores) can lead to submission errors.

:mag: Common Pitfalls & What to Check Next:

  • Server-Side Restrictions: Some hosting environments block outbound connections on port 443 (HTTPS) from certain parts of a website (like checkout pages) for security reasons. Check your server’s configuration or contact your hosting provider if you suspect this is the case. Consider using a different method to submit the form if server restrictions are in place.

  • Network Connectivity: Ensure your Magento server can successfully connect to the internet and access https://forms.hubspot.com.

  • HubSpot API Rate Limits: If you’re sending a high volume of requests, you might be exceeding HubSpot’s API rate limits. Check HubSpot’s API documentation for details on rate limits and strategies to handle them (e.g., batching requests or implementing delays).

  • HubSpot Form Errors: Check the HubSpot form’s settings for any potential errors or issues that might be preventing form submissions.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.