Skip to content

Instantly share code, notes, and snippets.

@ajmorris
Last active February 6, 2024 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajmorris/7593700 to your computer and use it in GitHub Desktop.
Save ajmorris/7593700 to your computer and use it in GitHub Desktop.
<?php
// Trying to create a function to use wp_remote_post() instead of cURL.
// Based off this example, http://developers.hubspot.com/docs/methods/forms/submit_form
public function send_hubspot_data( $first_name, $last_name, $email ) {
$hubspotutk = $_COOKIE['hubspotutk']; // grab the cookie from the visitors browser.
$ipaddress = $_SERVER['REMOTE_ADDR']; // IP address.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ipaddress,
'pageUrl' => $_SERVER['request_uri'],
'pageName' => 'Checkout'
);
$hs_context_json = json_encode( $hs_context );
// Populate variables with values from the form.
$str_post = "firstname=" . urlencode( $first_name )
. "&lastname=" . urlencode( $last_name )
. "&email=" . urlencode( $email )
. "&hs_context=" . urlencode( $hs_context_json );
$portal_id = get_option( 'aj_hubspot_hub_id' );
$form_guid = get_option( 'aj_hubspot_form_guid' );
$post_url = 'https://forms.hubspot.com/uploads/form/v2/' . $portal_id . '/' . $form_guid . '';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 1000,
'redirection' => 100,
'httpversion' => '1.0',
'blocking' => true,
'headers' => 'Content-Type: application/x-www-form-urlencoded',
'body' => $str_post,
'cookies' => array()
)
);
}
@benmay
Copy link

benmay commented Apr 4, 2014

Exactly what I was looking for. Thanks for sharing!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment