【问题标题】:Form API from Hubspot won't recognise my custom formHubspot 的表单 API 无法识别我的自定义表单
【发布时间】:2019-07-14 23:15:36
【问题描述】:

我正在使用本文档中的 PHP 设置 Hubspot 表单 API: https://developers.hubspot.com/docs/methods/forms/submit_form

在我将它实现到我的 Wordpress 网站之前(也使用自定义 HTML 表单,不是由 Wordpress 插件开发的),我一直在通过 XAMPP 在本地对其进行测试。

基本上,代码的作用是来自 Hubspot 的表单 API 将从自定义表单中捕获数据并将其发送到 Hubspot 以生成与我的自定义表单中的每个字段名称相对应的新联系人详细信息。

我制作了一个简单的 HTML 表单:

<!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>

以及包含 API 的 PHP:

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

?>

我不知道我做错了什么,因为我不是这方面的专家。在 hubspot 中,php 文件可以捕获名为 "Zen Test" 的页面源,但它不会识别其他任何内容,并且在提交后,页面会抛出错误为 "undefined variable firstname 、姓氏、电子邮件”。

请帮忙。

【问题讨论】:

    标签: php forms hubspot


    【解决方案1】:

    您正在使用这些变量构建字符串:

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

    但你没有在我能看到的任何地方创建它们。

    你需要添加:

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

    靠近页面顶部或至少在您构建字符串的块之前。

    【讨论】:

    • 它仍然无法识别我的变量并给我这个错误:``` 注意:未定义的索引:第 4 行 /Applications/XAMPP/xamppfiles/htdocs/hubspot.php 中的 hubspotutk 注意:未定义的索引:第 15 行 /Applications/XAMPP/xamppfiles/htdocs/hubspot.php 中的名字 注意:未定义索引:第 16 行 /Applications/XAMPP/xamppfiles/htdocs/hubspot.php 中的姓氏 注意:未定义索引:/Applications/XAMPP 中的电子邮件/xamppfiles/htdocs/hubspot.php 第 17 204 行```
    • 更新:对不起,我现在可以识别变量,但是我仍然无法摆脱 hubspotutk,与上面的错误相同
    • 您是否将 cookie 设置到访问者浏览器中?
    • 嗨 John.M,有什么好的资源可以将 cookie 注入浏览器吗?
    猜你喜欢
    • 2019-09-15
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多