You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已集成Contact Form 7与Zoho API,如何实现附件数据同步至Zoho CRM?

Fixing Contact Form 7 Attachment Sync to Zoho CRM

Got it, let's tackle this attachment sync issue between Contact Form 7 and Zoho CRM. Your current code handles text fields perfectly, but the file upload part needs key adjustments—Zoho CRM doesn't accept raw file paths or names directly; you need to upload the file as an attachment to the Lead record via their dedicated Attachments API. Here's the solution:

Key Issues in Your Current Code

  • You're pulling file data from $posted_data['file-714'], which only gives the file name, not the actual uploaded file's temporary path on your server.
  • Zoho CRM doesn't accept file paths in Lead fields; you must use the Leads/{record_id}/Attachments endpoint to upload the file to the specific Lead after creating it.

Modified Working Code

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();
            // Get actual uploaded file details (replaces your $documents line)
            $uploaded_files = $submission->get_uploaded_files();
            $file_data = $uploaded_files['file-714']; // Match your CF7 file field name
            $file_path = $file_data['tmp_name'];
            $file_name = $file_data['name'];
            $file_type = $file_data['type'];

            // Extract text fields as before
            $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-628'][0];
            $noc = $posted_data['checkbox-730'][0];
            
            // Fix undefined variables (adjust date format to match Zoho's requirements)
            $zoho_date = date('Y-m-d');
            $zc_gad = isset($_GET['gclid']) ? $_GET['gclid'] : '';

            // Fetch Zoho Access Token (your existing logic works here)
            $url = "https://accounts.zoho.com/oauth/v2/token";
            $query = "refresh_token=xxxxxxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&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);
            $accessToken = json_decode($result, true);

            // Prepare Lead data (removed 'Documents' since we'll upload as an attachment)
            $data = array(
                "First_Name"=>$first_name, 
                "Last_Name"=>$last_name, 
                "Email"=>$email, 
                "Phone"=>$phone, 
                "Description"=>$message, 
                "Budget" => $budget, 
                'Subscribed_Newsletter' => $checkbox, 
                'Company' => $noc, 
                "Lead_Date"=>$zoho_date, 
                '$gclid'=>$zc_gad
            );
            $encodedData = array('data' => array($data));

            // Create Lead record in Zoho CRM
            $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));
            $lead_result = curl_exec($ch);
            curl_close($ch);

            // Parse Lead ID to attach the file
            $lead_response = json_decode($lead_result, true);
            if (isset($lead_response['data'][0]['details']['id'])) {
                $lead_id = $lead_response['data'][0]['details']['id'];

                // Upload attachment to the created Lead
                $attachment_url = "https://www.zohoapis.com/crm/v2/Leads/{$lead_id}/Attachments";
                $ch_attach = curl_init();
                
                // Use CURLFile for proper multipart/form-data upload
                $post_fields = array(
                    'file' => new CURLFile($file_path, $file_type, $file_name)
                );

                curl_setopt($ch_attach, CURLOPT_URL, $attachment_url);
                curl_setopt($ch_attach, CURLOPT_HTTPHEADER, array(
                    'Authorization: Zoho-oauthtoken ' . $accessToken['access_token']
                ));
                curl_setopt($ch_attach, CURLOPT_POST, 1);
                curl_setopt($ch_attach, CURLOPT_POSTFIELDS, $post_fields);
                curl_setopt($ch_attach, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch_attach, CURLOPT_TIMEOUT, 30);

                $attachment_result = curl_exec($ch_attach);
                curl_close($ch_attach);

                // Log attachment upload status for debugging
                $log_content = "Lead ID: {$lead_id}\nAttachment Upload Result: {$attachment_result}\n";
                file_put_contents("lead-zoho-attachment-status.txt", $log_content . "\n\n", FILE_APPEND);
            }

            // Update existing Lead creation logging
            $leadSaving = print_r($lead_result, true);
            $leadSaving .= "\r\n" . $email . "\r\nrequest quote";
            file_put_contents("lead-zoho-entry-status.txt", $leadSaving . "\r\n\r\n", FILE_APPEND);
        }
    }
}

Important Notes

  1. File Field Match: Ensure 'file-714' exactly matches the name attribute of your CF7 file upload field.
  2. Zoho Permissions: Verify your Zoho OAuth token has permissions to create Leads and add Attachments (check your Zoho CRM API settings).
  3. PHP Version: CURLFile requires PHP 5.5+ (standard for modern WordPress installs). If you're on an older version, use @$file_path instead (note: this is deprecated).
  4. Date Format: Adjust $zoho_date if Zoho expects a different date/time format (e.g., date('c') for ISO 8601).

内容的提问来源于stack exchange,提问作者Aman Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:12:44