Form Submission API & Context

Occasional Contributor | Diamond Partner


I am using this code to submit HS form, the form submission is working but the $hubspotutk value is always null.

Any ideas what I need to fix here to pass the HubSpot cookie with the form submission

<?php $firstname = "ismail"; $email = ""; $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' => '', 'pageName' => 'Example Title' ); $hs_context_json = json_encode($hs_context); echo $hs_context_json; //Need to populate these variable with values from the form. $str_post = "firstname=" . urlencode($firstname) . "&email=" . urlencode($email) . "&hs_context=" . urlencode($hs_context_json); //Leave this one be //replace the values in this URL with your portal ID and your form GUID $endpoint = '<>'; $ch = @curl_init(); @curl_setopt($ch, CURLOPT_POST, true); @curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post); @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); echo $status_code . " " . $response; ?>
book a meeting with me
1 Reply 1
HubSpot Moderator

Hi @Ismail

Is there an actual form that’s submitting data to this script? And does the page with that form have the HubSpot tracking code installed on it? If you’re just running that as a standalone script there wouldn’t be a hubspotutk cookie to grab. Also, the hubspotutk cookie is generated by the HubSpot tracking code, so if the code isn’t on the page with the form that cookie might not be generated.