0

I'm setting up the Hubspot form API, using PHP from this documentation: https://developers.hubspot.com/docs/methods/forms/submit_form

Before I implement it to my Wordpress site (also using custom HTML form, not developed by Wordpress plugins) I have been testing it locally via XAMPP.

Basically what the code does is the form API from Hubspot will capture the data from the custom form and send it to Hubspot to generate a new contact detail corresponding to every field name from my custom form.

I have made a simple HTML form :

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="hubspot.php" method="post">
  First name:<br>
  <input type="text" name="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname" ><br>
  Email:<br>
   <input type="email" name="email" ><br><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>

and the PHP that contains the API:

<?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.
$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;

?>

I dont know what have I done wrong because im no expert at this. Within hubspot, the php file can capture the page source named "Zen Test" but it won't recognize anything else, and after the submission, the page throws an error as "undefined variable firstname, lastname, email".

Please help.

Zen
  • 11
  • 1

1 Answers1

0

You are building the string with those variables:

$str_post = "firstname=" . urlencode($firstname) 
    . "&lastname=" . urlencode($lastname) 
    . "&email=" . urlencode($email) 

but you aren't creating them anywhere that I can see.

You need to add:

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email= $_POST["email"];

near the top of the page or at least before the block where you build the string.

John.M
  • 335
  • 1
  • 10
  • it still cant recognize my variables and give me this error: ``` Notice: Undefined index: hubspotutk in /Applications/XAMPP/xamppfiles/htdocs/hubspot.php on line 4 Notice: Undefined index: firstname in /Applications/XAMPP/xamppfiles/htdocs/hubspot.php on line 15 Notice: Undefined index: lastname in /Applications/XAMPP/xamppfiles/htdocs/hubspot.php on line 16 Notice: Undefined index: email in /Applications/XAMPP/xamppfiles/htdocs/hubspot.php on line 17 204 ``` – Zen Jul 15 '19 at 02:32
  • updates: sorry I got the variables recognizabled now, however i still cant get rid of the hubspotutk, same as the error above – Zen Jul 15 '19 at 02:35
  • are you setting the cookie into the visitors browser? – John.M Jul 15 '19 at 02:55
  • Hi John.M, any good resource to inject the cookie into the browser? – Zen Jul 15 '19 at 07:53