APIs & Integrations

JGlasspool
Member

Connecting WPForms to HubSpot Forms API

Hi

 

I am trying send form submissions through a Wordpress site to HubSpot forms API but nothing is getting through.

 

Here is my code. Can you see any obvious errors in it:

 

add_action( 'wpforms_process_complete', 'send_to_hubspot', 5, 4 );
/**
 * Integrate WPForms with CRM
 *
 * @author Bill Erickson
 * @link https://www.billerickson.net/contact-form-integration/
 * 
 * @param array $fields
 * @param array $entry
 * @param array $form_data
 * @param int $entry_id
 */

function send_to_hubspot($fields, $form_data) {
    
	$hubspot_api_key = 'XXXXXXXXXXXXXXXXXXX';
    $hubspot_portal_id = 'XXXXXXXX';	
	
	// Keep In Touch form (WPForms ID = 105)
	if( $form_data['id'] == '105' ) {
		
		$hubspot_form_guid = '{FORM_ID}';	

		$hubspot_data = array(
			'firstname'                => $fields['6']['value'],
			'lastname'               => $fields['14']['value'],
			'email'               => $fields['1']['value'],
			'zip'               => $fields['12']['value'],		
		);
		
	}
	
	// Finance Offer Contact Form (WPForms ID = 5617)
	if( $form_data['id'] == '5617' ) {
	
	$hubspot_form_guid = '{FORM_ID}';
		
		$hubspot_data = array(
			'firstname'                => $fields['3']['value'],
			'lastname'               => $fields['9']['value'],
			'phone'               => $fields['12']['value'],
			'email'               => $fields['4']['value'],
			'Postal_code'               => $fields['5']['value'],
			'dealer_id'               => $fields['8']['value_raw'],
			'message'               => $fields['16']['value']
		);
		
	}
	
	// Test Drive Form (WPForms ID = 2951)
	if( $form_data['id'] == '2951' ) {
		
		$hubspot_form_guid = '{FORM_ID}';	

		$hubspot_data = array(
			'firstname'                => $fields['3']['value'],
			'lastname'               => $fields['9']['value'],
			'phone'               => $fields['12']['value'],
			'email'               => $fields['4']['value'],
			'location'               => $fields['28']['value'],
			'dealer_id'               => $fields['29']['value'],
			'extra_info'               => $fields['30']['value'],		
		);
		
	}
	
	$api_url = "https://api.hsforms.com/submissions/v3/integration/secure/submit/{$hubspot_portal_id}/{$hubspot_form_guid}";	
	
	$response = wp_remote_post( 
		$api_url,		
		array( 
			'headers' => array(
                'Content-Type'  => 'application/json',
                'Authorization' => 'Bearer ' . $hubspot_api_key,
            ),
			'body' => wp_json_encode($hubspot_data),
		) 
	);
	
	// Check for errors
    if ( is_wp_error( $response ) ) {
        error_log( 'HubSpot API error: ' . $response->get_error_message() );
    } else {
        $body = wp_remote_retrieve_body( $response );
        $data = json_decode( $body );

        // Log the response for debugging purposes
        error_log( 'HubSpot API response: ' . print_r( $data, true ) );
    }
		
}

 

 

0 Upvotes
1 Reply 1
dsmarion
Contributor

Connecting WPForms to HubSpot Forms API

Do you have the HubSpot tracking code installed on your WP site?

0 Upvotes