APIs & Integrations

marcusjwilson
Member

Submitting form: 204 code, but submission is not visible in HubSpot

Hello! We’ve written code to submit form data via Submit form API. The idea is that whenever our form is submited, its data is sent to HubSpot.

We get a 204 result from HubSpot, however, the test submissions we’ve added are not visible in HubSpot.

Here is the PHP code we use for adding the submissions:

/**
 * Sends form data to HubSpot.
 *
 * Returns true if the data was sent successfully (the status code is either
 * 204 or 302), and false otherwise. The status code is set to the
 * statusCode member variable, and the response is stored into the response
 * member variable.
 *
 * @param $hubspotForm array Form data formatted as in $forms static
 * member variable.
 * @return bool True if the data was successfully sent, false otherwise
 */
 
public function sendFormToHubSpot($hubspotForm) {
    $fields = $this->mapFields($hubspotForm['fieldMap']);
    
    $hs_context      = [
        'ipAddress' => $_SERVER['REMOTE_ADDR']
    ];
    
    if (isset($_COOKIE['hubspotutk'])) {
        $hs_context['hutk'] = $_COOKIE['hubspotutk'];
    }
    
    if (isset($hubspotForm['page_id'])) {
        $page_id = $hubspotForm['page_id'];
        $hs_context['pageUrl'] = get_permalink($page_id);
        $hs_context['pageName'] = get_the_title($page_id);
    }
    else {
        $hs_context['pageUrl'] = $_SERVER['HTTP_REFERER'];
        $hs_context['pageName'] = 'UNKNOWN PAGE NAME (page_id not set)';
    }
    
    $fields['hs_context'] = json_encode($hs_context);
    
    $post_params = [];
    foreach ($fields as $key => $value) {
        $post_params[] = $key . '=' . urlencode($value);
    }
    
    $str_post = implode('&', $post_params);
    
    $endpoint = 'https://forms.hubspot.com/uploads/form/v2/' .
        self::$portalId . '/' . $hubspotForm['formId'];
    
    $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);
    $this->response = @curl_exec($ch);
    $this->statusCode = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    @curl_close($ch);
    
    return in_array((int)$this->statusCode, [204, 302]);
}

The parts of code not shown seem to work correctly. $hubspotForm is our internal structure to map fields in our local form to HubSpot ($hubspotForm['fieldMap'], used by mapFields) and keeps the HubSpot formId ($hubspotForm['formId']), $portalId static variable keeps the portal ID. All these seem to work correctly. page_id is set to the correct page ID, and the functions get_permalink and get_the_title correctly return the URL and title of the page containing the form. The fields generated seem correct.

The data is actually sent. The return value is 204, which is a success code. But I can’t see the data in the back-end, when I open ‘View Submissions’.

Here’s what we actually pass (which seem correct; the references to our site are redacted, please tell if you need some additional information):

HS content (before json-encoding): Array
(
    [ipAddress] => 11.111.111.111
    [pageUrl] => http://redactedredactedre.da/ctedred/
    [pageName] => Redacted
)

POST data (before urlencoding): Array
(
    [firstname] => Test First name
    [lastname] => Test Last Name
    [company] => Test Company
    [phone] => Test Daytime Phone
    [email] => redac@tedred.ac
    [comment] => Test enquiry 4

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    [hs_context] => {"ipAddress":"11.111.111.111","pageUrl":"http:\/\/redactedredactedre.da\/ctedred\/","pageName":"Redacted"}
)

POST data (url-encoded): firstname=Test+First+name&lastname=Test+Last+Name&company=Test+Company&phone=Test+Daytime+Phone&email=redac%40tedred.ac&comment=Test+enquiry+4%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetur+adipisicing+elit%2C+sed+do+eiusmod+tempor+incididunt+ut+labore+et+dolore+magna+aliqua.+Ut+enim+ad+minim+veniam%2C+quis+nostrud+exercitation+ullamco+laboris+nisi+ut+aliquip+ex+ea+commodo+consequat.+Duis+aute+irure+dolor+in+reprehenderit+in+voluptate+velit+esse+cillum+dolore+eu+fugiat+nulla+pariatur.+Excepteur+sint+occaecat+cupidatat+non+proident%2C+sunt+in+culpa+qui+officia+deserunt+mollit+anim+id+est+laborum.&hs_context=%7B%22ipAddress%22%3A%2211.111.111.111%22%2C%22pageUrl%22%3A%22http%3A%5C%2F%5C%2Fredactedredactedre.da%5C%2Fctedred%5C%2F%22%2C%22pageName%22%3A%22Redacted%22%7D

Endpoint: https://forms.hubspot.com/uploads/form/v2/REDACTE/REDACTED-REDA-CTED-REDA-CTEDREDACTED

Response: <empty>

Status code: 204

My guess it that something might not be set up correctly in our HubSpot account.

We also use a developing site, which is on a different domain from our main site. Maybe this could be a problem?

0 Upvotes
2 Replies 2
Dadams
HubSpot Employee
HubSpot Employee

Submitting form: 204 code, but submission is not visible in HubSpot

Hi @marcusjwilson

Are you not seeing the submissions in HubSpot at all? Even if there was a problem with the data, you should still see an empty submission listed in the view submissions list, so if you’re getting a 204 there should be something in the list (even if there’s no data).

Is the email address that you’re using for this a test record? We do have a limit on the number of form submissions that a contact can have, so if there are a large number of submissions for that existing record, form submissions could be dropped. More details on that are here:

Contact and Form Limits

Limits for contact records and form submissions.

0 Upvotes
marcusjwilson
Member

Submitting form: 204 code, but submission is not visible in HubSpot

Many thanks -

The form data eventually did show up in our HubSpot account. But it seemed to take 6 hours between submitting the form via the API/getting the 204 alert, and then seeing the form data in our HubSpot account.

We’re testing again today, and the form data seems to appear in HubSpot much quicker - pretty instantaneous now. Not sure what was going on before.

Best wishes
Marcus

0 Upvotes