How to add leads to a campaign in zoho crm using api

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
  1.  // Get the form data
  2.     $submission = WPCF7_Submission::get_instance();
  3.     
  4.     if ($submission) {
  5.         $posted_data = $submission->get_posted_data();

  6.         $zoho_form_first_name = isset($posted_data['your-name']) ? $posted_data['your-name'] : '';
  7.         $zoho_form_last_name = isset($posted_data['your-last']) ? $posted_data['your-last'] : '';
  8.         $zoho_form_company = isset($posted_data['your-company']) ? $posted_data['your-company'] : '';
  9.         $zoho_form_email = isset($posted_data['your-email']) ? $posted_data['your-email'] : '';
  10.         $zoho_form_company_size = isset($posted_data['radio-420']) ? $posted_data['radio-420'] : '';
  11.         $zoho_form_industry = isset($posted_data['radio-427']) ? $posted_data['radio-427'] : '';
  12.         $zoho_form_expert = isset($posted_data['radio-323']) ? $posted_data['radio-323'] : '';
  13.         $zoho_form_expert_time = isset($posted_data['radio-499']) ? $posted_data['radio-499'] : '';
  14.         $zoho_form_expert_engagement = isset($posted_data['radio-624']) ? $posted_data['radio-624'] : '';
  15.         $zoho_form_online = isset($posted_data['radio-354']) ? $posted_data['radio-354'] : '';
  16.         $zoho_form_hear_about_us = isset($posted_data['radio-452']) ? $posted_data['radio-452'] : '';
  17.         $zoho_form_genai = isset($posted_data['radio-694']) ? $posted_data['radio-694'] : '';

  18.         // Prepare the data to send to Zoho CRM
  19.         $data = array(
  20.             "data" => [ 
  21.                 [   
  22.                     "Owner" => [
  23.                             "name" => "John",
  24.                             "id" => "696191000000317001"
  25.                     ],
  26.                     "First_Name" => $zoho_form_first_name,
  27.                     "Last_Name" => $zoho_form_last_name,
  28.                     "Industry"=> $zoho_form_industry[0],
  29.                     "Company" => $zoho_form_company,
  30.                     "Email" => $zoho_form_email,
  31. "Lead_Status" => "Not Contacted",
  32. "Lead_Source" => "Website",
  33.                     "Description" => "1. Company Size: {$zoho_form_company_size[0]}
  34.                                       2. Expert Requirement: {$zoho_form_expert[0]}
  35.   
  36.                                       3. Expert Required in: {$zoho_form_expert_time[0]}
  37.   
  38.                                       4. Expert Engagement Required: {$zoho_form_expert_engagement[0]}
  39.   
  40.                                       5. Comfortable with working remotely: {$zoho_form_online[0]}
  41.   
  42.                                       6. How did you hear about us: {$zoho_form_hear_about_us[0]}
  43.   
  44.                                       7. Generative AI Try: {$zoho_form_genai[0]}"
  45.                                       
  46.                                       
  47.                 ]
  48.             ]
  49.         );

  50.         // Convert array to JSON
  51.         $json_data = json_encode($data);

  52.         // Get Zoho Access Token
  53.         $access_token = get_zoho_access_token();

  54.         // Zoho CRM API URL for Leads/Contacts
  55.         $zoho_api_url = 'https://www.zohoapis.in/crm/v7/Leads'; // Change 'Leads' to 'Contacts' if needed

  56.         // Set up HTTP POST request headers
  57.         $args = array(
  58.             'body'        => $json_data,
  59.             'headers'     => array(
  60.                 'Authorization' => 'Zoho-oauthtoken ' . $access_token,
  61.                 'Content-Type'  => 'application/json',
  62.             ),
  63.         );

  64.         // Send data to Zoho CRM using wp_remote_post
  65.         $response = wp_remote_post($zoho_api_url, $args);

  66.         if (is_wp_error($response)) {
  67.             error_log('Zoho CRM API error: ' . $response->get_error_message());
  68.         } else {
  69.             error_log('Zoho CRM API response: ' . wp_remote_retrieve_body($response));
  70.            

  71.             // Parse the response to get the created lead's ID
  72.             $response_body = wp_remote_retrieve_body($response);
  73.             $response_data = json_decode($response_body, true);
  74.             log_errors('lead posted'. $response_body);
  75.             if (isset($response_data['data'][0]['id'])) {
  76.                 $lead_id = $response_data['data'][0]['id'];

  77.                
  78.             }
  79.         }
  80.     }
  81. }




    Access your files securely from anywhere

        Zoho Developer Community




                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now