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;
?>