integrate hubspot forms submit with laravel/guzzle
SOLVE
Hi @CHolt123, Allthough I do not know where it goes wrong with your code, I thought it might be helpfull to share my PHP script which we use to submit to a specific form:
// Default settings
$baseURL = 'https://forms.hubspot.com/uploads/form/v2/';
// Portal ID of client
$portalId = '';
// Hubspot Form ID (grabbed from URL)
$hubspot_form = '';
$params = '';
// Loop through all post values and build URL params, example: FirstName=Teun&LastName=Rutten
foreach($post as $key => $value) {
if ( ! empty( $value ) ) {
$name = $key;
$params = $params . $name . '=' . urlencode($value) . "&";
}
}
$hubspotutk = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : ''; //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => $post['url'],
'pageName' => isset($post['page']) ? $post['page'] : ''
);
$hs_context_json = json_encode($hs_context);
$params = $params . '&hs_context=' . urlencode($hs_context_json);
// Prepare URL
$endpoint = $baseURL . $portalId . '/' . $hubspot_form;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
curl_close($ch);
return $response . ' ' . $status_code;
}
Learn more about HubSpot by following me on LinkedIn or YouTube
✅ Did my answer solve your issue? Help the community by marking it as the solution.
integrate hubspot forms submit with laravel/guzzle
SOLVE
Hi @CHolt123, Allthough I do not know where it goes wrong with your code, I thought it might be helpfull to share my PHP script which we use to submit to a specific form:
// Default settings
$baseURL = 'https://forms.hubspot.com/uploads/form/v2/';
// Portal ID of client
$portalId = '';
// Hubspot Form ID (grabbed from URL)
$hubspot_form = '';
$params = '';
// Loop through all post values and build URL params, example: FirstName=Teun&LastName=Rutten
foreach($post as $key => $value) {
if ( ! empty( $value ) ) {
$name = $key;
$params = $params . $name . '=' . urlencode($value) . "&";
}
}
$hubspotutk = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : ''; //grab the cookie from the visitors browser.
$ip_addr = $_SERVER['REMOTE_ADDR']; //IP address too.
$hs_context = array(
'hutk' => $hubspotutk,
'ipAddress' => $ip_addr,
'pageUrl' => $post['url'],
'pageName' => isset($post['page']) ? $post['page'] : ''
);
$hs_context_json = json_encode($hs_context);
$params = $params . '&hs_context=' . urlencode($hs_context_json);
// Prepare URL
$endpoint = $baseURL . $portalId . '/' . $hubspot_form;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Log the response from HubSpot as needed.
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
curl_close($ch);
return $response . ' ' . $status_code;
}
Learn more about HubSpot by following me on LinkedIn or YouTube
✅ Did my answer solve your issue? Help the community by marking it as the solution.