<?php
//Process a new form submission in HubSpot in order to create a new Contact.
$firstname = $lastname = $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' => 'http://localhost/mother.html',
'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these variable with values from the form.
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';
$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;
?>
It seems that the code that you have used for the form submission is working fine. The only thing that I have noticed in the code snippet is that you have made the email variable empty ( "" ) string and it can be cause of in proper data.
Try once with valid email value and it will work for sure.
I replaced the variables now the code works fine and successfully capture the data and send to hubspot. However, it still throws me an error with hubspotutk, looks like I have to add something to the cookie ? can you help
<?php
//Process a new form submission in HubSpot in order to create a new Contact.
$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://localhost/mother.html',
'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these variable with values from the form.
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email= $_POST["email"];
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';
$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;
?>
When looking to use the hubspotutk cookie, you've got to first installed the HubSpot tracking code (Learn more here: Install the HubSpot tracking code). This is because the hubspotutk cookie is placed in the user's browser by the HubSpot tracking code. This tracking code must be installed on the page that the form is on.
To verify if the HubSpot tracking code has been installed and that it's firing correctly, you can refer to this useful documentation: verify HubSpot tracking code.
In this case, could you confirm that with the tracking code present, you're seeing the error?
It seems that the code that you have used for the form submission is working fine. The only thing that I have noticed in the code snippet is that you have made the email variable empty ( "" ) string and it can be cause of in proper data.
Try once with valid email value and it will work for sure.
I replaced the variables now the code works fine and successfully capture the data and send to hubspot. However, it still throws me an error with hubspotutk, looks like I have to add something to the cookie ? can you help
<?php
//Process a new form submission in HubSpot in order to create a new Contact.
$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://localhost/mother.html',
'pageName' => 'Zen Test'
);
$hs_context_json = json_encode($hs_context);
//Need to populate these variable with values from the form.
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email= $_POST["email"];
$str_post = "firstname=" . urlencode($firstname)
. "&lastname=" . urlencode($lastname)
. "&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/6123547/0051538d-2665-4988-8a18-2c449de75ae3';
$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;
?>
When looking to use the hubspotutk cookie, you've got to first installed the HubSpot tracking code (Learn more here: Install the HubSpot tracking code). This is because the hubspotutk cookie is placed in the user's browser by the HubSpot tracking code. This tracking code must be installed on the page that the form is on.
To verify if the HubSpot tracking code has been installed and that it's firing correctly, you can refer to this useful documentation: verify HubSpot tracking code.
In this case, could you confirm that with the tracking code present, you're seeing the error?