Ryan Lebo
Ryan Lebo

Reputation: 45

Send gravity form entries to 3rd party API

I'm currently trying to send information from Gravity Froms to a 3rd party API. I understand there is the gform_after_submission hook in Gravity Forms to send information to a 3rd party 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 ) );
}

I am trying to use this but I also need to send information based on different methods provided by the API. In this case I'm creating a form for users to either enter a new rewards card or transfer a card. Basically I have to look at my form and send a call to a method to check the old card number, send a call to add / update a customer, so on and so forth.

Now using Gravity Forms gform_after_submission, how can I achieve everything I need to do to enter the information into the correct Method on the API. Please understand this would be the first time I'm sending information from Gravity Forms to an API like this.

Upvotes: 2

Views: 5452

Answers (1)

Parth Goswami
Parth Goswami

Reputation: 348

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 );

Hope i've explained well & helps you a bit to understand things better..you can change form data as per your need good luck.

Upvotes: 5

Related Questions