Sending form data to 3rd party API

We all know we’re a little limited by not being able to assign our own name attributes for the form fields we create. This is particularly troublesome when we want to have 1 function be able to handle multiple forms with fields all named differently in each… as well as potentially containing different fields in the first place.

My case is that I need to send each form’s (around 8 or 9 different forms) data to my CRM’s API. I wrote a function for the gform_after_submission hook that maps the fields by using each field’s Admin Label as the key it will be submitted to the CRM with. So for example, if your CRM is expecting the First Name field’s value as $data['first-name'], but Gravity Forms names it 1, all you have to do is set the Admin Label to `first-name’ - the function will map it to the Gravity-Form-generated name and assign the correct value to the correct key.

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

	$crm_success = false;

	// default values for payload to be passed to CRM API
	$data = array(
		'email' => '',
		'opt_in_confirmation' => false
	);

    foreach($form['fields'] as $item ) {
          $label = $item->adminLabel;
          $data[$label] = rgar($entry, $item->id);
	}

    // only bother calling API if we have a valid email address
    if(filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {

        /* ==================================== */
        /* Do your own  API operations here */
        /* ===================================== */

        $url = "https://api.mycrm.net/" . CRM_ACCT_ID . "/members/signup";
        $response = crm_post($url, $data);
        $responseArr = json_decode($response, true);

        $crm_status = null;
        if (json_last_error() == JSON_ERROR_NONE) {
            $crm_status = isset($responseArr[count($responseArr)-1]['status']) ?: false;
            $crm_success = ('a' === $crm_status);
        }

        /* ====================================== */
        /* end API operations */
        /* ====================================== */
    }


   // alert somebody if API call fails
   if(true !==  $crm_success) {
        handle_crm_error($response);
   } 


  /* Payload sent to API */
  print '<pre>Payload:';
  print_r( $data );
  print '</pre>';

  /* Response from API */
  print '<pre>Response:';
  print_r( $responseArr );
  print '</pre>';

}

I’m kind of new to WordPress & Gravity Forms and definitely not super well-versed in PHP. i’d love any input anyone has (feel free to shoot all the holes in it you can find) or suggestions for improvements.

Also, I know there is a way to achieve the whole mapping thing through Gravity Form’s API, but that documentation went right over my head. But if anyone cares to show the way to do this the 100%-gravity-forms-way, I’d love to see it.

Otherwise, I feel like this may come in handy for people with several, differently-structured forms that all need to eventually send their data to the same API endpoint .

1 Like

Thank you for sharing that. I’m sure it will be useful for someone. It’s an interesting approach to the problem.

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.