I’m working on integrating HubSpot with my Magento store to automatically send customer data when orders are completed. The integration runs on the order confirmation page, and I’ve verified that my $payload_data variable contains all the required customer information. However, I’m getting an empty $api_response and can’t figure out what’s wrong. The cURL request doesn’t seem to be executing properly. Do I need to configure something specific in Magento to allow outbound API calls from the success page?
Note: I’ve masked the actual portal and form IDs in the code below.
<?php
// Send customer data to HubSpot when order is placed
$tracking_token = $_COOKIE['hubspotutk']; // Get visitor tracking cookie
$client_ip = $_SERVER['REMOTE_ADDR']; // Capture IP address
$context_data = array(
'hutk' => $tracking_token,
'ipAddress' => $client_ip,
'pageUrl' => 'https://www.mystore.com/checkout/success/',
'pageTitle' => 'Order Complete - MyStore'
);
$context_json = json_encode($context_data);
// Build form data from customer information
$payload_data = "first_name=" . urlencode($customer_fname)
. "&last_name=" . urlencode($customer_lname)
. "&email_address=" . urlencode($customer_email)
. "&phone_number=" . urlencode($customer_phone)
. "&street_address=" . urlencode($billing_street)
. "&city_name=" . urlencode($billing_city)
. "&state_region=" . urlencode($billing_state)
. "&country_code=" . urlencode($billing_country)
. "&hs_context=" . urlencode($context_json);
// HubSpot form submission endpoint
$api_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, $payload_data);
@curl_setopt($curl_handle, CURLOPT_URL, $api_url);
@curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
@curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$api_response = @curl_exec($curl_handle);
@curl_close($curl_handle);
echo $api_response;
?>
This sounds like an SSL or timeout issue. Add these two lines to your cURL config: curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false) and curl_setopt($curl_handle, CURLOPT_TIMEOUT, 30). I’ve hit the same problem with third-party APIs in Magento - the default cURL settings are way too restrictive. Also check if your host blocks outbound connections on port 443. Some do this by default. Quick test: run a simple cURL request to HubSpot from your server’s command line. If that works but your code doesn’t, it’s probably Magento environment permissions or your PHP config, not the API call.
Try file_get_contents with stream context instead of curl - Magento’s curl can be weird sometimes. Double-check your portal-id and form-guid in the URL too. HubSpot just returns empty responses when the IDs are wrong. Quick debug tip: throw in a var_dump($payload_data) to make sure your data looks right before sending.
You’ve got a timing issue here. You’re calling curl_close() before checking for errors or grabbing the response code - move that close statement to the end after you’ve captured what you need. The real problem is probably that your success page loads before Magento finishes processing the customer data. I’ve seen this tons of times where the order object isn’t fully saved when the redirect happens. Ditch running this on the success page and hook into the sales_order_place_after event instead. That guarantees the order data’s actually committed to the database. One more fix - add curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0') since some servers will reject requests without a proper user agent.
first, fix your curl error handling - ditch those @ symbols and add curl_error($curl_handle) to see what’s actually breaking. also, your CURLOPT_HTTPHEADER needs to be 'Content-Type: application/x-www-form-urlencoded' not just the content type value.
Check your server’s firewall and hosting provider’s outbound connection policies. Most shared hosts block external API calls by default for security. I had this exact problem with a Magento store - the cURL request was silently failing because my host required whitelisting specific domains for outbound connections. Contact your host and ask them to whitelist forms.hubspot.com. Also try adding curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true) since HubSpot sometimes returns redirects. Double-check your portal ID and form GUID are correct - HubSpot returns empty responses for invalid endpoints instead of proper error messages. Test your endpoint URL directly in a browser or Postman first to confirm it’s accepting submissions.