Sending form data to HubSpot using Drupal

Sending form data to HubSpot using Drupal

The Hubspot module handles a lot of use-cases such as embedding forms and sending data to HubSpot using webforms.

I came across a requirement that we needed to submit data to HubSpot after someone made a purchase on the site (using Drupal commerce). Using HubSpot’s API, its very easy to submit form data to HubSpot with Drupal’s tools.

Find out more about the HubSpot API here: http://developers.hubspot.com/docs/overview

// set url parameters
$hubspot_portalid = '123456'; // can grab from hubspot module setting,
$hubspot_formid = '1234-5678-2345678-345667'; // recommend variable_get for these
// set form parameters
$data = array(
  'firstname' => $customer_first_name,
  'lastname' => $customer_last_name,
  'email' => $customer_email,
);
$options = array(
  'method' => 'POST',
  'headers' => array(
    'Content-Type' =>  'application/x-www-form-urlencoded',
  ),
  'data' => http_build_query($data, '', '&'),
);
$url = 'https://forms.hubspot.com/uploads/form/v2/' . $hubspot_portalid . '/' . $hubspot_formid;
// send and handle request
$response = drupal_http_request($url, $options);
if ($response->code == '204') { // success
  watchdog('mymodule_hubspot', 'Successfully submitted form to Hubspot');
}
else { // error
  $response_array = (array) $response;
  watchdog('mymodule_hubspot', 'Error submitting form to Hubspot. ' . print_r($response_array, TRUE));
}

Comments are closed.