0

我正在尝试在 Magento 的成功页面上向 Hubspot 提交表单。我已经确认变量 $str_post 包含所有需要的信息。不幸的是,我无法确定为什么 $response 是空的。似乎没有建立 CURL 连接,但我似乎无法确定原因。除了加载页面之外,我还需要做些什么来触发 CURL 连接吗?

请注意,我已从 URL 中删除了 GUI/Form ID。

<?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' => 'https://www.myfoodstorage.com/onestepcheckout/',
        'pageTitle' => 'MyFoodStorage.com Cart Checkout'
    );
$hs_context_json = json_encode($hs_context);

//Need to populate these varilables with values from the form.
$str_post = "firstname=" . urlencode($firstname)
        . "&lastname=" . urlencode($lastname)
        . "&email=" . urlencode($email)
        . "&phone=" . urlencode($telephone)
        . "&address=" . urlencode($street)
        . "&city=" . urlencode($city)
        . "&state=" . urlencode($region)
        . "&country=" . urlencode($country)
        . "&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/GUI-ID/FORM-ID';

$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('application/x-www-form-urlencoded'));
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = @curl_exec($ch);  //Log the response from HubSpot as needed.
@curl_close($ch);
echo $response;

?>
4

1 回答 1

0

成功提交到 Forms API 将返回 204(无内容)响应,因此响应正文中不会有任何内容。您可以使用 curl_getinfo 获取状态码。

$response = @curl_exec($ch);  //Log the response from HubSpot as needed.
echo @curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);
echo $response;
于 2013-02-28T21:46:01.100 回答