【发布时间】: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 、姓氏、电子邮件”。
请帮忙。
【问题讨论】: