Magento& Hubspot - 在成功页面上提交表格

时间:2013-02-27 20:11:33

标签: php magento curl

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

请注意我已从网址中删除了GUI /表单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;

?>

1 个答案:

答案 0 :(得分: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;