简体   繁体   中英

Form API from Hubspot won't recognise my custom form

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM