APIs & Integrations

No aplicable

Forms API- No response

I am relatively new at PHP. I need to connect one of my external forms to Hubspot so new submissions create new contacts. I followed the instructions here: http://developers.hubspot.com/docs/methods/forms/submit_form

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>
0 Me gusta
4 Respuestas 4
Dadams
HubSpot Employee
HubSpot Employee

Forms API- No response

@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.

$curl_errors = curl_error($ch);
@curl_close($ch);
echo $status_code . " " . $response . " " . $curl_errors;
0 Me gusta
No aplicable

Forms API- No response

actually adding this fixes it:

@curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

but that makes it so that it isn’t a secure connection any more. Is there a better way to deal with this?

0 Me gusta
Dadams
HubSpot Employee
HubSpot Employee

Forms API- No response

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:

Ubuntu 10.04/CURL: How do I fix/update the CA Bundle?

ubuntu, curl

0 Me gusta
No aplicable

Forms API- No response

Aha! Just what I needed. I am getting cURL error: “SSL certificate problem: self signed certificate in certificate chain”.

I solved it via this guide: http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

You’re a lifesaver!

0 Me gusta