首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Forminator + Hubspot工作流

Forminator + Hubspot工作流
EN

WordPress Development用户
提问于 2023-03-21 15:58:31
回答 1查看 28关注 0票数 0

我需要向客户端发送Forminator表单数据到Hubspot工作流。这似乎不起作用

代码语言:javascript
复制
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);

哈勃黑子中没有添加任何联系人,工作流也不会被触发

有什么想法吗?

EN

回答 1

WordPress Development用户

发布于 2023-03-23 12:39:46

对于任何想知道的人,我自己解决了:

代码语言:javascript
复制
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);
}
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/414814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档