Home > Back-end >  I want to add contact form 7 data to zoho crm with cutom code in wordpress. I tried but not find any
I want to add contact form 7 data to zoho crm with cutom code in wordpress. I tried but not find any

Time:07-29

I already added the code on my function.php file by applying zoho crm api but the data is not transferring on ZOHO crm. please help to resolve it because it is urgent

I called the data through the access token provided by Zoho crm, then i create API v2 of web API and generate client id and client secret. I don't know where i am making error

add_action('wpcf7_mail_sent','brainium_cf7_api_sender');

function brainium_cf7_api_sender(){

$title = $contact_form->title;

     if( $title === 'Contact form 1') {
       $submission = WPCF7_Submission::get_instance();

    if( $submission ){
      $posted_data = $submission->get_posted_data();

      $first_name = $posted_data['first-name'];
      $last_name = $posted_data['last-name'];
      $email = $posted_data['your-email'];
      $phone = $posted_data['Phone-no'];
      $message = $posted_data['your-message'];
      $budget = $posted_data['budget'];
      $checkbox = $posted_data['checkbox-993'];

                $auth = 'fa738eaef1becee890f8935f65169e99';
                $refreshToken = "1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a";
                //get the last date and time of refresh token generation
                
                //get the access token
                $url = "https://accounts.zoho.com/oauth/v2/token";
                $query = "refresh_token=1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a&client_id=1000.PJ66M19LBVNAYS7XXP3XP9RU5MXJAH&client_secret=6995e79c891ec5444440b9d07bc2a00c535c074cf3&grant_type=refresh_token";
                
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
                $result = curl_exec($ch);
                curl_close($ch);
                //get the token from the JSON in result
                $accessToken =  json_decode($result, true);
                //echo ($accessToken['access_token']); 
                //die();        
                
                $data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget"  => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
                //$data = json_encode($data);
                
                $encodedData = array();
                $encodedData['data'][0] = $data;
                
                //var_dump($data);
                //echo(json_encode($encodedData));
                //die();
                //che


                $url ="https://www.zohoapis.com/crm/v2/Leads";
                $headers = array(
                                    'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
                                    'Content-Type:application/json'
                                );

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 30);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
                $result = curl_exec($ch);
                curl_close($ch);
                //echo $result;
                /**** End zoho CRM ****/
                $success = true;
                $msg = 'Done';
                
                $leadSaving = print_r($result, true);
                $leadSaving = $leadSaving . "\r\n" . $email;
                $leadSaving = $leadSaving . "\r\n" . "request quote";
                file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
                file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND);      }

} }

CodePudding user response:

I tried and getting the following SUCCESS response

{"data":[{"code":"SUCCESS","details":{"Modified_Time":"2022-07-27T17:28:36 05:30","Modified_By":{"name":"Sourav Sinha","id":"1153488000000068001"},"Created_Time":"2022-07-27T17:28:36 05:30","id":"1153488000046560019","Created_By":{"name":"Sourav Sinha","id":"1153488000000068001"}},"message":"record added","status":"success"}]}
[email protected]
request quote

CodePudding user response:

<?php
$first_name = 'first-name';
$last_name = 'last-name';
$email = '[email protected]';
$phone = '9876543210';
$message = 'your-message';
$budget = '1000';
$checkbox = '1';

          $auth = 'fa738eaef1becee890f8935f65169e99';
          $refreshToken = "1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a";
          //get the last date and time of refresh token generation
          
          //get the access token
          $url = "https://accounts.zoho.com/oauth/v2/token";
          $query = "refresh_token=1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a&client_id=1000.PJ66M19LBVNAYS7XXP3XP9RU5MXJAH&client_secret=6995e79c891ec5444440b9d07bc2a00c535c074cf3&grant_type=refresh_token";
          
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_TIMEOUT, 30);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
          $result = curl_exec($ch);
          curl_close($ch);
          //get the token from the JSON in result
          $accessToken =  json_decode($result, true);
          //echo ($accessToken['access_token']); 
          //die();        
          
          $data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget"  => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
          //$data = json_encode($data);
          
          $encodedData = array();
          $encodedData['data'][0] = $data;
          
          //var_dump($data);
          //echo(json_encode($encodedData));
          //die();
          //che


          $url ="https://www.zohoapis.com/crm/v2/Leads";
          $headers = array(
                              'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
                              'Content-Type:application/json'
                          );

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_TIMEOUT, 30);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
          $result = curl_exec($ch);
          curl_close($ch);
          //echo $result;
          /**** End zoho CRM ****/
          $success = true;
          $msg = 'Done';
          
          $leadSaving = print_r($result, true);
          $leadSaving = $leadSaving . "\r\n" . $email;
          $leadSaving = $leadSaving . "\r\n" . "request quote";
          file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
          file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND);

CodePudding user response:

This is the code used in functions.php file

add_action('wpcf7_mail_sent','brainium_cf7_api_sender');
function brainium_cf7_api_sender($contact_form){
    $title = $contact_form->title;

    if( $title === 'Contact form 1') {
       $submission = WPCF7_Submission::get_instance();

        if( $submission ){
            $posted_data = $submission->get_posted_data();

            $first_name = $posted_data['first-name'];
            $last_name = $posted_data['last-name'];
            $email = $posted_data['your-email'];
            $phone = $posted_data['Phone-no'];
            $message = $posted_data['your-message'];
            $budget = $posted_data['budget'];
            $checkbox = $posted_data['checkbox-194'][0];

            $auth = 'fa738eaef1becee890f8935f65169e99';
            $refreshToken = "1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a";
            //get the last date and time of refresh token generation
            
            //get the access token
            $url = "https://accounts.zoho.com/oauth/v2/token";
            $query = "refresh_token=1000.3c0d5bb96a00e9438d132d94a316a72b.2d5261b7e33c42614c08f1bd4e9a0f1a&client_id=1000.PJ66M19LBVNAYS7XXP3XP9RU5MXJAH&client_secret=6995e79c891ec5444440b9d07bc2a00c535c074cf3&grant_type=refresh_token";
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
            $result = curl_exec($ch);
            curl_close($ch);
            //get the token from the JSON in result
            $accessToken =  json_decode($result, true);
            //echo ($accessToken['access_token']); 
            //die();        
            
            $data = array("First_Name"=>$first_name, "Last_Name"=>$last_name, "Email"=>$email, "Phone"=>$phone, "Description"=>($message), "Budget"  => $budget, 'Subscribed_Newsletter' => $checkbox, "Lead_Date"=>$zoho_date, '$gclid'=>$zc_gad);
            //$data = json_encode($data);
            
            $encodedData = array();
            $encodedData['data'][0] = $data;
            
            //var_dump($data);
            //echo(json_encode($encodedData));
            //die();
            //che


            $url ="https://www.zohoapis.com/crm/v2/Leads";
            $headers = array(
                                'Authorization: Zoho-oauthtoken '.$accessToken['access_token'],
                                'Content-Type:application/json'
                            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($encodedData));
            $result = curl_exec($ch);
            curl_close($ch);
            //echo $result;
            // End zoho CRM
            $success = true;
            $msg = 'Done';
            
            $leadSaving = print_r($result, true);
            $leadSaving = $leadSaving . "\r\n" . $email;
            $leadSaving = $leadSaving . "\r\n" . "request quote";
            file_put_contents("lead-zoho-entry-status.txt", $leadSaving, FILE_APPEND);
            file_put_contents("lead-zoho-entry-status.txt", "\r\n\r\n", FILE_APPEND); 
        }
    } 
}
  • Related