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

WordPress中通过cURL发送Elementor表单数据失败问题排查

问题排查与解决指导:Elementor表单数据无法同步到Wolkvox API

核心问题

整合Elementor表单与Wolkvox API的代码执行后,数据未同步到Wolkvox平台,已确认替换了API地址和token。

代码中存在的明确错误

1. 表单名称校验逻辑错误

原Elementor逻辑中,$record->get_form_settings('form_name')用于获取表单的名称配置项,你的代码错误地将表单名Testing作为参数传入,导致无法正确匹配表单:

// 错误写法
$form_name = $record->get_form_settings( 'Testing' );

// 正确写法
$form_name = $record->get_form_settings( 'form_name' );

若不修正,这个判断会直接返回,后续同步代码根本不会执行。

2. 数据格式不匹配Wolkvox API要求

Wolkvox API要求POST数据是数组包裹的JSON对象(示例为[{...}]),但你的代码仅生成单个JSON对象,且字段名与API要求不匹配:

// 错误写法
$data = json_encode([
    "customer_name" => $fields['name'], 
    "customer_id" => $fields['customerID'], 
    "tel1" => $fields['tel1'], 
]);

// 正确写法(匹配API数组结构与字段名)
$data = json_encode([
    [
        "customer_name" => $fields['name'], 
        "customer_id" => $fields['customerID'], 
        "opt1" => $fields['tel1'], // API要求字段为opt1,不是tel1
    ]
]);

3. WordPress环境下的curl兼容性问题

WordPress推荐使用内置的wp_remote_post替代原生curl,避免主机禁用curl、权限限制等兼容性问题,同时方便调试:

// 替换原curl代码为以下内容
$response = wp_remote_post( '你的Wolkvox API地址', [
    'headers' => [
        'wolkvox-token' => '你的有效token',
        'Content-Type' => 'application/json',
    ],
    'body' => $data,
    'timeout' => 30,
]);

// 调试日志(仅测试用,上线后删除)
if ( is_wp_error( $response ) ) {
    error_log( 'API请求错误: ' . $response->get_error_message() );
} else {
    error_log( 'API响应: ' . wp_remote_retrieve_body( $response ) );
}

额外排查步骤

  • 字段ID匹配验证:确认Elementor表单中字段的ID(不是前端显示的标签名)确实是name、customerID、tel1,可通过打印字段数组排查:
    error_log( print_r( $fields, true ) ); // 查看实际获取到的字段数据
    
  • API有效性验证:用Postman等工具直接调用Wolkvox API,确认地址、token、数据格式是否能正常创建记录,排除API本身的问题。
  • 开启WordPress调试:在wp-config.php中设置WP_DEBUG = true、WP_DEBUG_LOG = true,查看wp-content/debug.log中的错误信息,定位具体问题。
  • 移除前端输出:代码中的echo $response会破坏Elementor表单的提交流程,需替换为日志输出或直接删除。

修正后的完整代码

function send_to_external_api( $record, $handler ) {
    // 获取表单名称
    $form_name = $record->get_form_settings( 'form_name' );

    // 仅处理指定表单
    if ( 'Testing' !== $form_name ) {
        return;
    }

    // 提取表单字段
    $raw_fields = $record->get( 'fields' );
    $fields = [];
    foreach ( $raw_fields as $id => $field ) {
        $fields[ $id ] = $field['value'];
    }

    // 格式化符合Wolkvox要求的JSON数据
    $data = json_encode([
        [
            "customer_name" => $fields['name'] ?? '', 
            "customer_id" => $fields['customerID'] ?? '', 
            "opt1" => $fields['tel1'] ?? '', 
        ]
    ]);

    // 使用WordPress内置函数发送请求
    $response = wp_remote_post( '你的Wolkvox API地址', [
        'headers' => [
            'wolkvox-token' => '你的有效token',
            'Content-Type' => 'application/json',
        ],
        'body' => $data,
        'timeout' => 30,
    ]);

    // 调试日志(上线时注释或删除)
    if ( is_wp_error( $response ) ) {
        error_log( 'Wolkvox API请求失败: ' . $response->get_error_message() );
    } else {
        error_log( 'Wolkvox API响应内容: ' . wp_remote_retrieve_body( $response ) );
    }
}
add_action( 'elementor_pro/forms/new_record', 'send_to_external_api', 10, 2 );

内容的提问来源于stack exchange,提问作者Diane Rendel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 15:37:11