我需要向客户端发送Forminator表单数据到Hubspot工作流。这似乎不起作用
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);哈勃黑子中没有添加任何联系人,工作流也不会被触发
有什么想法吗?
发布于 2023-03-23 12:39:46
对于任何想知道的人,我自己解决了:
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);
}https://wordpress.stackexchange.com/questions/414814
复制相似问题