4

This is how to save data to zoho crm api using laravel, the process of this is the data will be check before uploading to crm to avoid duplicate data on zoho crm.

    public function importToZoho()
    {
      $publicUser = User::withRole('User')->get();
      $count = 1;
      $auth="YOUR TOKEN ID";
    
      //Insert Records to CRM     
      $xml =   '<?xml version="1.0" encoding="UTF-8"?>';
      $xml .=  '<Leads>'; 
      foreach($publicUser as $user){
    
        if(SohoUploaded::where('user_id', $user->id)->exists()){
            echo "File Exists";
        }else{
            $uploadedZoho = new SohoUploaded();
            $uploadedZoho->user_id = $user->id;
            $uploadedZoho->email = $user->email;
            $uploadedZoho->first_name = $user->first_name;
            $uploadedZoho->last_name = $user->last_name;
            $uploadedZoho->save();
    
            $xml .= '<row no="'.$count++.'">';
            $xml .= '<FL val="Lead Owner">Sales Shift</FL>';
            $xml .= '<FL val="Company">'.$user->company.'</FL>';
            $xml .= '<FL val="First Name">'. $user->first_name.'</FL>';
            $xml .= '<FL val="Last Name">'. $user->last_name.'</FL>';
            $xml .= '<FL val="Email">'. $user->email.'</FL>';
            $xml .= '<FL val="Phone">'. $user->phone.'</FL>';
    
            $xml .= '</row>';
        }
    
      }      
      $xml .= '</Leads>';                       
      $url ="https://crm.zoho.com/crm/private/xml/Leads/insertRecords?duplicateCheck=1&";
      $query="authtoken=".$auth."&scope=crmapi&xmlData=".$xml;
      $ch = curl_init();
      /* set url to send post request */
      curl_setopt($ch, CURLOPT_URL, $url);
      /* allow redirects */
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      /* return a response into a variable */
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      /* times out after 30s */
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
      /* set POST method */
      curl_setopt($ch, CURLOPT_POST, 1);
      /* add POST fields parameters */
      curl_setopt($ch, CURLOPT_POSTFIELDS, $query);// Set the request as a POST FIELD for curl.
    
      //Execute cUrl session
      $response = curl_exec($ch);
      curl_close($ch);
    
      return redirect()->to('/public-user')->withSuccess('New leads uploaded to Zoho CRM');
    
    }

Let me know if you have some question regarding this post, im happy to help.

ZohoCoder
  • 385
  • 5
  • 15

0 Answers0