【问题标题】:Contact form 7 data sending to API联系表格 7 数据发送到 API
【发布时间】:2021-08-19 07:40:45
【问题描述】:

我想在提交时将我的 7 数据联系人发送到 API。为此,我使用了以下代码。

add_action('wpcf7_mail_sent','crazy_cf7_api_sender');

function crazy_cf7_api_sender( $contact_form ){
    $title = $contact_form -> title;

    echo "Test";
    if( $title == 'Committee Club'){
    
        $submission = WPCF7_Submission::get_instance();

        
        if( $submission ){
            $posted_data = $submission->get_posted_data();
                $committee_club = $posted_data['committee-club'];
                $language = $posted_data['language'];
                $Intrest = $posted_data['Intrest'];

         
                $url = 'https://staging.walee.pk/api/tracking/newWordPressHook';

        $args = array(
'method' => 'POST',
'orderId' => 220,
);

$response = wp_remote_post( $url, $args );

// error check
if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
}
else {
   echo 'Response: <pre>';
   print_r( $response );
   echo '</pre>';
}
        }
    }
}

我得到了参考

https://www.youtube.com/watch?v=QOjpqHFM9v8

https://wp-kama.com/function/wp_remote_post

但是当我提交表单时,我收到以下错误消息:

{code: "invalid_json", message: "响应不是有效的 json 响应。"}

有人可以帮帮我吗?

谢谢

【问题讨论】:

标签: php wordpress api wordpress-rest-api


【解决方案1】:

您不应该在代码中使用echo 任何内容,因为钩子 wpcf7_mail_sent 在 Ajax 中被调用并且它需要 JSON 输出。

请删除任何echovar_dumpprint_r 或任何其他从您的代码中打印任何内容的内容

 echo 'Response: <pre>';
   print_r( $response );
   echo '</pre>';

【讨论】:

  • 感谢@Andrea 重播。我删除了 echo 但我仍然收到同样的错误。
  • 顶部还有echo "Test";。您需要删除所有 echo/var_dump/print_r
  • 您还可以使用 Chrome 控制台或 Firefox 控制台检查对 ajax 的网络调用,以准确了解您正在打印的内容
  • 我已经从文件中删除了所有的 echo 和 var 转储。现在错误已删除。请告诉我如何检查数据是否在 api 中发布?
猜你喜欢
  • 2018-04-16
  • 2018-04-27
  • 1970-01-01
  • 2013-04-22
  • 2021-01-27
  • 2016-02-07
  • 2011-10-29
  • 2014-08-27
相关资源
最近更新 更多