WordPress中Contact Form7同步Zoho CRM失败,请求排查代码问题
Contact Form 7 表单数据同步Zoho CRM失败排查与修复
我尝试通过WordPress自定义代码将Contact Form 7的表单数据同步到Zoho CRM,已经在functions.php中部署了基于Zoho CRM API v2的代码,用refresh token获取access token,也生成了对应的client id和client secret,但数据始终传不进去,不知道哪里出问题,附上代码求排查:
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 = 'xxxxxxxxxxxx'; $refreshToken = "xxxxxxxxxxxx"; //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=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); //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); } } }
排查出的核心问题
- 未接收钩子参数:
wpcf7_mail_sent钩子会传递表单对象作为第一个参数,但你的函数brainium_cf7_api_sender()没有接收,导致$contact_form->title直接报错,后续代码根本无法执行。 - 未定义变量:
$zoho_date和$zc_gad没有赋值就塞进数据数组,会导致数据结构不合法,Zoho API会拒绝请求。 - CURL错误未处理:没有检查CURL请求的执行状态,也没有处理API返回的错误响应,无法定位token获取或数据提交阶段的问题。
- 字段名可能不匹配:Zoho CRM的Lead字段需要严格使用API名称(不是后台显示名),比如
$gclid前面的$符号是多余的,应该改为gclid;另外Subscribed_Newsletter需要确认是否是正确的API字段名。 - 日志路径问题:
file_put_contents用相对路径会将日志写到服务器根目录(而非主题目录),大概率没有权限写入,导致无法查看错误日志。
修复后的代码
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 = isset($posted_data['first-name']) ? sanitize_text_field($posted_data['first-name']) : ''; $last_name = isset($posted_data['last-name']) ? sanitize_text_field($posted_data['last-name']) : ''; $email = isset($posted_data['your-email']) ? sanitize_email($posted_data['your-email']) : ''; $phone = isset($posted_data['Phone-no']) ? sanitize_text_field($posted_data['Phone-no']) : ''; $message = isset($posted_data['your-message']) ? sanitize_textarea_field($posted_data['your-message']) : ''; $budget = isset($posted_data['budget']) ? sanitize_text_field($posted_data['budget']) : ''; $checkbox = isset($posted_data['checkbox-993']) ? 1 : 0; // 复选框转布尔值,匹配Zoho字段类型 // 配置Zoho认证信息 $refreshToken = "xxxxxxxxxxxx"; $clientId = "xxxxxxxxxxxx"; $clientSecret = "xxxxxxxxxxxx"; // 获取Access Token $tokenUrl = "https://accounts.zoho.com/oauth/v2/token"; $tokenParams = http_build_query([ 'refresh_token' => $refreshToken, 'client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'refresh_token' ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tokenUrl); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $tokenParams); // 开启SSL验证(生产环境建议开启) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); $tokenResult = curl_exec($ch); // 检查CURL错误 if(curl_errno($ch)){ $errorMsg = 'Token获取CURL错误: ' . curl_error($ch); log_zoho_error($errorMsg, $email); curl_close($ch); return; } curl_close($ch); $accessTokenData = json_decode($tokenResult, true); // 检查Token是否获取成功 if(!isset($accessTokenData['access_token'])){ $errorMsg = 'Token获取失败: ' . print_r($accessTokenData, true); log_zoho_error($errorMsg, $email); return; } $accessToken = $accessTokenData['access_token']; // 准备Zoho Lead数据,修复未定义变量 $zohoDate = current_time('Y-m-d H:i:s'); // 使用WordPress当前时间,匹配Zoho日期格式 $zcGad = isset($_GET['gclid']) ? sanitize_text_field($_GET['gclid']) : ''; // 获取URL中的gclid $leadData = array( "First_Name" => $first_name, "Last_Name" => $last_name, "Email" => $email, "Phone" => $phone, "Description" => $message, "Budget" => $budget, "Subscribed_Newsletter" => $checkbox, "Lead_Date" => $zohoDate, "gclid" => $zcGad // 去掉多余的$符号 ); $encodedData = array('data' => array($leadData)); // 提交数据到Zoho CRM $leadUrl = "https://www.zohoapis.com/crm/v2/Leads"; $headers = array( 'Authorization: Zoho-oauthtoken ' . $accessToken, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $leadUrl); 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)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); $leadResult = curl_exec($ch); // 检查CURL错误 if(curl_errno($ch)){ $errorMsg = 'Lead提交CURL错误: ' . curl_error($ch); log_zoho_error($errorMsg, $email); curl_close($ch); return; } curl_close($ch); // 记录结果到主题目录的日志文件 log_zoho_result($leadResult, $email); } } } // 封装日志写入函数,确保路径正确 function log_zoho_error($errorMsg, $email){ $logPath = get_stylesheet_directory() . '/zoho-lead-errors.txt'; $logContent = date('Y-m-d H:i:s') . " - 邮箱: $email - 错误信息: $errorMsg\r\n\r\n"; file_put_contents($logPath, $logContent, FILE_APPEND); } function log_zoho_result($result, $email){ $logPath = get_stylesheet_directory() . '/zoho-lead-results.txt'; $logContent = date('Y-m-d H:i:s') . " - 邮箱: $email - 返回结果: $result\r\n\r\n"; file_put_contents($logPath, $logContent, FILE_APPEND); }
额外注意事项
- 替换代码中的
xxxxxxxxxxxx为你的实际Zoho认证信息 - 登录Zoho CRM后台,进入设置->API->字段API名称,确认Lead模块的字段API名称完全匹配代码中的键名
- 测试表单提交后,查看主题目录下的
zoho-lead-errors.txt和zoho-lead-results.txt,根据日志进一步排查问题 - 如果你的Zoho账户是国际版以外的区域(比如中国区),需要将API域名改为对应的区域域名(比如
www.zohoapis.com.cn)
内容的提问来源于stack exchange,提问作者Aman Gupta
相关产品推荐
相关产品推荐

