Gform_after_submission to third party api [RESOLVED]

I am working with Gravity Forms to send that form Data to an external API with a post request.

This is what I have so far. It is using the gform_after_submission and then using that information to send to the third party API with a cURL request.

 add_action( 'gform_after_submission_1', 'post_to_third_party', 10, 2 );
 function post_to_third_party( $entry, $form ) {
  
    $body = array(
         'first_name' => rgar( $entry, '22' ),
         'last_name' => rgar( $entry, '23' ),
         'email' => rgar( $entry, '16' ),
         'phone' => rgar( $entry, '18' ),
         'streetadd' => rgar( $entry, '6' ),
         'city' => rgar( $entry, '8' ),
         'state' => rgar( $entry, '10' ),
         'postalcode' => rgar( $entry, '19' ),
         'dob' => rgar( $entry, '13' ),
         'contact' => rgar( $entry, '14' ),
         'product' => rgar( $entry, '1' ),
     )
 }
 $curl = curl_init();
 
 curl_setopt_array($curl, array(
   CURLOPT_URL => 'URL to my API',
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => '',
   CURLOPT_MAXREDIRS => 10,
   CURLOPT_TIMEOUT => 0,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
   CURLOPT_CUSTOMREQUEST => 'PUT',
   CURLOPT_POSTFIELDS =>'{
     "tenant": "07484e7f-b9b7-4d12-8c7e-3fa19d2e39a1",
	"first_name": first_name,
	"middle_initial": "L",
	"last_name": last_name,
	"origin": "Website",
	"business_type": "Personal",
	"business_name": "Acme Incorporated",
	"country_code": "US",
	"lob": "Home",
	"payment_type": "Credit Card",
	"lead_format": "json",
	"data": {
		"Version": "7.0",
		"Proposer": {
			"Business": false,
			"Forename": first_name,
			"Surname": last_name,
			"DateOfBirth": dob

		}
	}
}
 }',
   CURLOPT_HTTPHEADER => array(
     'X-API-Key: *APIKEY*',
     'Content-Type: application/json'
   ),
 ));
 
 $response = curl_exec($curl);
 
 curl_close($curl);
 echo $response;
 }

When I go to test this, it isn’t sending over anything from the form to my API. I thought it was similar to the Javascript "get elementbyID and then referring to that like I did with the rgar here. However, nothing is sending over from my form and I am not able to see any error messages in my log. I appreciate any help I can get with this.

You define the $body array with all the field-related values but then do not use that object in your curl request? I don’t think that is necessarily why you’re not seeing connection to your API but certainly something to revise once you solve the connectivity issue.

Depending on what access you have to the API backend, testing with a site like requestbin or the mocking features of Postman can be valuable to rule out any of the API system, CORS restrictions, etc.

If your API request structure is as straight forward as your example shows, you may want to look at the Gravity Forms webhook add-on or Gravity Flow’s outgoing webhook step type. Both provide you with key/value mapping options for fields. Gravity Flow’s provides some additional features where your request structure may involve nested elements or you want to map values from the API response back into fields.

Thanks for the help. It will connect to the API with no issues as long as I put in the values in quotes. However, when I put them in as the variables. Now I am trying to figure out how to refer to the “body” in that cURL request.

Hi Jerry, this basically looks correct, but does seem strange you’re defining $body and then not submitting it anywhere. As Jamie points out.

This is probably better suited as a question to the provider of that API. It’s not a Gravity form issue since you’ve chosen the right hook.

You’re right to hook into _after_submission and do a curl. Now it depends on what that API expects. Every API will be different in how they need you to structure your request data and it’s crucial that you get it 100% right. Reach out to them and ask them why it’s failing.

And to echo Jamie, download Postman for free and try sending that off as a request. Once you have Postman setup it makes it much easier to test out requests before you do the work of mapping fields from Gravity into an API request.

Hope this helps!

1 Like

I actually figured it out. I was able to use the rgar and refer to the entryID in the form I am trying to submit to the API. I put it in the first_name in the JSON file. Thanks for the help.

1 Like