HubSpot / hubspot-php

HubSpot PHP API Client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Form submission fails with '400 Bad Request'

neilf184 opened this issue · comments

I have a Symfony website which, until now, has been using the ryanwinchester/hubspot-php package to submit hubspot forms. In order to comply with the requirement to cease using API Token authentication and switch to using a Private Application Token I've had to replace the ryanwinchester/hubspot-php package with hubspot/hubspot-php.

Unfortunately I can't get form submission to work! I submit the form using:-
$response = $this->application->getHubspot()
->forms()
->submit(getenv("HS_FORM_PORTAL"), getenv("HS_DEMO_DOWNLOAD_FORM"), $formData)
;

This works perfectly using ryanwinchester but fails every time with hubspot/hubspot-php with otherwise identical code. I get:
Client error: POST https://forms.hubspot.com/submissions/v3/integration/submit/5163272/5479b330-36f5-4d7f-af9c-07b4e3c6138a resulted in a 400 Bad Request response:
{"status":"error","message":"The request is not valid","correlationId":"f5cb2915-39ac-4eab-9083-1a740fa708eb","errors":[ (truncated...)

It looks like it's using an undocumented endpoint. Coming from ryanwinchester/hubspot-php here is what I changed in the forms()->submit() function:

public function submit($portal_id, $form_guid, array $form)
{
    $endpoint = "https://api.hsforms.com/submissions/v3/integration/submit/{$portal_id}/{$form_guid}";

    $fields = [];
    $json = [];
    foreach($form as $key=>$val)
    {
        if($key == "hs_context")
            $json['context'] = json_decode($val);
        else
            $fields[] = ['objectTypeId' => '0-1', 'name'=>$key, 'value'=>$val];
    }
    $json['fields'] = $fields;

    return $this->client->request('post', $endpoint, ['json' => $json], null, false);
}

I worked around it by calling the api directly without using the hubspot/hubspot-php classes.