Form Submission API & Context


#1

Hello,

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 = "test123@123.com"; $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' => 'http://www.example.com/form-page', '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 = 'https://forms.hubspot.com/uploads/form/v2/465916/<>'; $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; ?>

#2

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.