0

I need to send Forminator form data to a Hubspot workflow for a client. This doesn't seem to be working

function send_form_data_to_hubspot($form_data) {

    $hubspot_api_key = 'MY_API_KEY';
    $hubspot_workflow_id = 'WORKFLOW_ID_HERE';
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://api.hubapi.com/automation/v3/workflows/{$hubspot_workflow_id}/enrollments/contacts",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($form_data),
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "Authorization: Bearer {$hubspot_api_key}"
        ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
        error_log("cURL Error #:" . $err);
        return false;
    } else {
        return true;
    }
    
}

add_action('forminator_custom_form_after_submission', function($entry, $form) {
    $form_data = array(
        'email' => $entry['email-1'],
        'first_name' => $entry['name-1'],
        'last_name' => $entry['name-2'],
        // add any additional fields you want to send to the Hubspot workflow
    );   
    send_form_data_to_hubspot($form_data);
}, 10, 2);

No contact is being added in Hubspot, and the workflow isn't triggered

Anyone have an ideas at all?

1
  • 1
    Forminator and Hubspot are both third-party services, so they don't fall under this stack's scope. Please check with the services' documentation and/or support systems for help on this. See here for which questions are on-topic. Thanks!
    – YourManDan
    Commented Mar 21, 2023 at 16:43

1 Answer 1

0

For anyone that want's to know, I fixed it myself:

function send_form_data_to_hubspot_pipeline($form_data, $form_id) {

// Set the API key and pipeline ID
$api_key = 'YOUR-HS-API-KEY';
$pipeline_id = 'YOUR-PIPELINE-ID';

// Create the contact data array
$contact_data = array(
    'properties' => array(
        array(
            'property' => 'email',
            'value' => $form_data['email-1']
        ),
        array(
            'property' => 'firstname',
            'value' => $form_data['name-1']
        ),
        //add any other fields here
    )
);

// Convert the data to JSON format
$json_data = json_encode($contact_data);

// Set the API endpoint URL
$endpoint_url = "https://api.hubapi.com/deals/v1/deal?hapikey={$api_key}";

// Set the request parameters
$request_args = array(
    'headers' => array(
        'Content-Type' => 'application/json'
    ),
    'body' => $json_data
);

// Send the request to the API endpoint
$response = wp_remote_post($endpoint_url, $request_args);

// Check for errors
if(is_wp_error($response)) {
    return false;
}

// Check the status code
$status_code = wp_remote_retrieve_response_code($response);
if($status_code != 200) {
    return false;
}

// Get the deal ID from the response
$response_body = wp_remote_retrieve_body($response);
$response_data = json_decode($response_body, true);
$deal_id = $response_data['dealId'];

// Add the deal to the pipeline
$pipeline_url = "https://api.hubapi.com/deals/v1/pipelines/{$pipeline_id}/deals/{$deal_id}?hapikey={$api_key}";
$pipeline_args = array(
    'headers' => array(
        'Content-Type' => 'application/json'
    ),
    'body' => '{}'
);
wp_remote_put($pipeline_url, $pipeline_args);

// Return true on success
return true;
}

function handle_form_submission($form_data) {
    send_form_data_to_hubspot_pipeline($form_data);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.