我正在尝试构建一个API链接以接受WordPress中带有一些客户数据(例如名字,姓氏,电子邮件)的HTTP POST请求。收到此数据后,代码应向自定义Hubspot表单发送另一个POST请求,以创建新联系人。
我可以使用curl -d "email=myemail@email.com" -X POST https://forms.hubspot.com/uploads/form/v2/{portalId}/{formGuid}
之类的curl请求在本地计算机上提交客户数据。这样就可以了。
接下来,我尝试在模板目录中创建一个全新的页面模板,并在WordPress仪表板中使用该模板创建页面。当我尝试使用curl -d "email=myemail@email.com" -X POST https://mywordpresssite.com/hubspot_submit_post/
之类的curl请求提交到此新页面时,它不起作用。我收到HTTP 200 OK响应,但是没有在Hubspot中创建新客户。
<?php
/* Template Name: Lead submit POST to Hubspot */
if ( ! defined( ‘ABSPATH’ ) ) {
exit;
}
$pageUrl = 'mywordpresssite.com/hubspot_submit_post.php';
$pageName = 'Leads';
$portalId = "some id";
$formGuid = "some guid";
//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' => $pageUrl,
'pageName' => $pageName
);
$hs_context_json = json_encode($hs_context);
$firstname = isset($_POST["first_name"]) ? $_POST["first_name"] : '';
$lastname = isset($_POST["last_name"]) ? $_POST["last_name"] : '';
$email = isset($_POST["email"]) ? $_POST["email"] : '';
$phone = isset($_POST["phone"]) ? $_POST["phone"] : '';
$company = isset($_POST["company"]) ? $_POST["phone"] : '';
$company_size = isset($_POST["company_size"]) ? $_POST["company_size"] : '';
$jobtitle = isset($_POST["jobtitle"]) ? $_POST["jobtitle"] : '';
$state = isset($_POST["state"]) ? $_POST["state"] : '';
$city = isset($_POST["city"]) ? $_POST["city"] : '';
$industry = isset($_POST["industry"]) ? $_POST["industry"] : '';
//Need to populate these variable with values from the form.
$str_post = "first_name=" . $firstname
. "&last_name=" . $lastname
. "&email=" . $email
. "&phone=" . $phone
. "&company=" . $company
. "&company_size=" . $company_size
. "&job_title=" . $jobtitle
. "&state=" . $state
. "&city=" . $city
. "&industry=" . $industry
. "&hs_context=" . urlencode($hs_context_json); //Leave this one be
header($str_post);
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/' . $portalId . '/' . $formGuid;
$data = wp_remote_post($endpoint, array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded'
),
'body' => $str_post
));
?>
感谢您的帮助。当我提交带有curl -d "email=myemail@email.com" -X POST https://mywordpresssite.com/hubspot_submit_post/
答案 0 :(得分:0)
由于某种原因,Wordpress似乎在此自定义php文件中使用ABSPATH
常量存在问题。我从代码开头删除了该支票。
此外,从这种类型的请求中获取hubspotutk
无效。
使用WP_DEBUG和WP_DEBUG_LOG查找PHP错误使查找这些问题的过程变得更加容易:https://wpadvancedads.com/finding-errors-with-wp_debug/
这是简化且有效的代码:
<?php
/* Template Name: Business.com POST to Hubspot */
$pageUrl = 'mywordpresssite.com/hubspot_submit_post.php';
$pageName = 'Business.com Leads';
$portalId = "some id";
$formGuid = "some guid";
//Process a new form submission in HubSpot in order to create a new Contact.
$firstname = isset($_POST["first_name"]) ? $_POST["first_name"] : '';
$lastname = isset($_POST["last_name"]) ? $_POST["last_name"] : '';
$email = isset($_POST["email"]) ? $_POST["email"] : '';
$phone = isset($_POST["phone"]) ? $_POST["phone"] : '';
$company = isset($_POST["company"]) ? $_POST["company"] : '';
$company_size = isset($_POST["company_size"]) ? $_POST["company_size"] : '';
$jobtitle = isset($_POST["jobtitle"]) ? $_POST["jobtitle"] : '';
$state = isset($_POST["state"]) ? $_POST["state"] : '';
$city = isset($_POST["city"]) ? $_POST["city"] : '';
$industry = isset($_POST["industry"]) ? $_POST["industry"] : '';
//Need to populate these variable with values from the form.
$str_post = "first_name=" . $firstname
. "&last_name=" . $lastname
. "&email=" . $email
. "&phone=" . $phone
. "&company=" . $company
. "&company_size=" . $company_size
. "&job_title=" . $jobtitle
. "&state=" . $state
. "&city=" . $city
. "&industry=" . $industry;
$endpoint = 'https://forms.hubspot.com/uploads/form/v2/' . $portalId . '/' . $formGuid;
$data = wp_remote_post($endpoint, array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded'
),
'body' => $str_post
));
?>