How to add leads to a campaign in zoho crm using api
I made a custom php script to send leads who filled website from to zoho crm. I also want the leads to enter a specific campaign. I can't seem to figure how to do it. It will be helpful even if you provide url, parameters, json which is working in postman, rest I can figure out.
Here's the code which I made
- // Get the form data
- $submission = WPCF7_Submission::get_instance();
-
- if ($submission) {
- $posted_data = $submission->get_posted_data();
- $zoho_form_first_name = isset($posted_data['your-name']) ? $posted_data['your-name'] : '';
- $zoho_form_last_name = isset($posted_data['your-last']) ? $posted_data['your-last'] : '';
- $zoho_form_company = isset($posted_data['your-company']) ? $posted_data['your-company'] : '';
- $zoho_form_email = isset($posted_data['your-email']) ? $posted_data['your-email'] : '';
- $zoho_form_company_size = isset($posted_data['radio-420']) ? $posted_data['radio-420'] : '';
- $zoho_form_industry = isset($posted_data['radio-427']) ? $posted_data['radio-427'] : '';
- $zoho_form_expert = isset($posted_data['radio-323']) ? $posted_data['radio-323'] : '';
- $zoho_form_expert_time = isset($posted_data['radio-499']) ? $posted_data['radio-499'] : '';
- $zoho_form_expert_engagement = isset($posted_data['radio-624']) ? $posted_data['radio-624'] : '';
- $zoho_form_online = isset($posted_data['radio-354']) ? $posted_data['radio-354'] : '';
- $zoho_form_hear_about_us = isset($posted_data['radio-452']) ? $posted_data['radio-452'] : '';
- $zoho_form_genai = isset($posted_data['radio-694']) ? $posted_data['radio-694'] : '';
- // Prepare the data to send to Zoho CRM
- $data = array(
- "data" => [
- [
- "Owner" => [
- "name" => "John",
- "id" => "696191000000317001"
- ],
- "First_Name" => $zoho_form_first_name,
- "Last_Name" => $zoho_form_last_name,
- "Industry"=> $zoho_form_industry[0],
- "Company" => $zoho_form_company,
- "Email" => $zoho_form_email,
- "Lead_Status" => "Not Contacted",
- "Lead_Source" => "Website",
- "Description" => "1. Company Size: {$zoho_form_company_size[0]}
-
- 2. Expert Requirement: {$zoho_form_expert[0]}
-
- 3. Expert Required in: {$zoho_form_expert_time[0]}
-
- 4. Expert Engagement Required: {$zoho_form_expert_engagement[0]}
-
- 5. Comfortable with working remotely: {$zoho_form_online[0]}
-
- 6. How did you hear about us: {$zoho_form_hear_about_us[0]}
-
- 7. Generative AI Try: {$zoho_form_genai[0]}"
-
-
- ]
- ]
- );
- // Convert array to JSON
- $json_data = json_encode($data);
- // Get Zoho Access Token
- $access_token = get_zoho_access_token();
- // Zoho CRM API URL for Leads/Contacts
- $zoho_api_url = 'https://www.zohoapis.in/crm/v7/Leads'; // Change 'Leads' to 'Contacts' if needed
- // Set up HTTP POST request headers
- $args = array(
- 'body' => $json_data,
- 'headers' => array(
- 'Authorization' => 'Zoho-oauthtoken ' . $access_token,
- 'Content-Type' => 'application/json',
- ),
- );
- // Send data to Zoho CRM using wp_remote_post
- $response = wp_remote_post($zoho_api_url, $args);
- if (is_wp_error($response)) {
- error_log('Zoho CRM API error: ' . $response->get_error_message());
- } else {
- error_log('Zoho CRM API response: ' . wp_remote_retrieve_body($response));
-
- // Parse the response to get the created lead's ID
- $response_body = wp_remote_retrieve_body($response);
- $response_data = json_decode($response_body, true);
- log_errors('lead posted'. $response_body);
- if (isset($response_data['data'][0]['id'])) {
- $lead_id = $response_data['data'][0]['id'];
-
- }
- }
- }
- }