将重力表单条目发送到第三方API

问题描述 投票:1回答:1

我目前正在尝试将Gravity Forms中的信息发送到第三方API。我知道Gravity Forms中有gform_after_submission钩子将信息发送到第三方API。

add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {

    $post_url = 'http://thirdparty.com';
    $body = array(
        'first_name' => rgar( $entry, '1.3' ), 
        'last_name' => rgar( $entry, '1.6' ), 
        'message' => rgar( $entry, '3' ),
    );
    GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );

    $request = new WP_Http();
    $response = $request->post( $post_url, array( 'body' => $body ) );
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}

我试图使用它,但我还需要根据API提供的不同方法发送信息。在这种情况下,我正在创建一个表单供用户输入新的奖励卡或转移卡。基本上我必须查看我的表单并发送一个方法来检查旧卡号,发送呼叫以添加/更新客户,依此类推。

现在使用Gravity Forms gform_after_submission,我怎样才能实现将信息输入到API上的正确方法所需的一切。请理解这是我第一次将Gravity Forms中的信息发送到这样的API。

php wordpress forms api gravity-forms-plugin
1个回答
2
投票
function post_to_third_party( $entry, $form ) {

//To fetch input inserted into your gravity form//
$old_card  = rgpost( 'input_6' );
$new_card  = rgpost( 'input_3' );

///to send that fetched data to third-party api///
$post_url = 'http://thirdparty.com/{APi request}';
$body = array(
        'old_card' => $old_card, 
        'new_card' => $new_card, 
 );

    GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );

    $request = new WP_Http();
    $response = $request->post( $post_url, array( 'body' => $body ) );

  //response from api whether card is exist or not///
  $res = json_decode($response['body'],true);

  ///here you can put your logic as per the response//
}
add_action( 'gform_after_submission_4', 'post_to_third_party', 10, 2 );

希望我能够很好地解释并帮助您更好地理解事物......您可以根据需要改变表单数据。

© www.soinside.com 2019 - 2024. All rights reserved.