The form processes on the same page, so I put the PHP at the top and set the Huspot processing script to run when submit is set. I replaced the endpoint URL But when I test it, I get “0” as the echoed response and status code and there is no submission in Hubspot. So I don’t think my page is connecting to the forms API. What am I doing wrong??
here’s a stripped down version of how I am setting it up:
<?php
if(isset($_POST['submit'])) {
$firstname = $_POST["firstname"];
$email = $_POST["email"];
$message = "Yes: {$email}";
//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://www.example.com/form-page',
'pageName' => 'Example Title'
);
$hs_context_json = json_encode($hs_context);
//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/{I PUT IN MY PORTAL ID/{I PUT IN MY FORM ID FROM THE URL}';
$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;
} else {
$message = "Log in.";
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php echo $message; ?>
<form action="test.php" method="post">
Name: <input type="text" name="firstname">
Email: <input type="text" name="email">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
@Erin_Taylor a response code of 0 usually means that cURL didn’t actually complete the response. You can get the last error from cURL using curl_error($ch) which may give you a better indication of why the request failed.
You’d need to update the Certificate Authority bundle that curl is using to validate SSL certificates. Part of that is covered in the “The proper fix” section of the page you linked, and exactly how you do that is going to depend on your server configuration, but there are some other general tips for doing that for Linux and Windows servers: