2013-02-27 77 views
0

我正在嘗試在Magento的成功頁面上將表單提交給Hubspot。我已經確認變量$ str_post包含所有需要的信息。不幸的是,我無法確定爲什麼$響應是空的。似乎CURL連接沒有被做,但我似乎無法確定爲什麼。除了加載頁面之外,我還需要做些什麼才能觸發CURL連接?Magento&Hubspot - 成功提交表單

請注意我已經從URL中刪除了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; 

?> 
+0

首先我希望這是在塊中,而不是在模板(.phtml)文件中。其次,你用@符號抑制了所有的錯誤報告,所以你永遠不會得到一個PHP錯誤。第三,看看curl_error():http://php.net/manual/en/function.curl-error.php – 2013-02-27 22:57:52

回答

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;