Category: PHP WordPress

In this blog post, we’ll cover how to send Gravity Forms Partial Data to a third-party API. This is an easy-to-follow tutorial that will have you sending data in no time.

This tutorial assumes that the reader has knowledge of PHP and WordPress. The code in this article was written for WordPress but can be easily modified for other platforms such as Drupal, Shopify, or Tumblr. We’ll use the Gravity Forms Partial Entries Add-On which will then process the form submission data before sending it to a third-party API.

Installation of the Gravity Forms Parial Data add-on and enabling form

Gravity Forms send Partial Data to API in PHP Enabled screenshot

Functions.php code

Next, in your functions.php file, you will need to add the two add_action calls Entry Saved and Entry Updated as referenced in the official gravity forms article. Remember to change ‘your_function_name’ to your custom name when creating your function.

// This will send data once the form has been saved on the server
add_action( 'gform_partialentries_post_entry_saved', 'your_function_name', 10, 2 );

// This will send data once more data has been added to the form
add_action( 'gform_partialentries_post_entry_updated', 'your_function_name', 10, 2 );

Creating a custom function to send data

Parameters and properties

We’re given two parameters by Gravity Forms to use when gathering data from the form:

$partial_entry Entry Object – Contains the entry field properties such as id, created_by etc. We can use these to assign our own variables like $email_address = $partial_entry[‘1’];

$form Form Object – Returns various properties such as id, title, description. For this tutorial, we will only be using the id value.

Building the function body

Now we understand what $form and $partial_entry serve we can start building our function. Again we will be following the article from Gravity Forms in terms of structure.

function send_to_custom_api_on_partial_entry_saved( $partial_entry, $form ) {

        // Only get data from status 'partial'
 	$partial_entry['status'] = 'partial';
}

Now we can assign some custom variables to the $partial_entry variables. You can check the ids of each input within WordPress.

function send_to_custom_api_on_partial_entry_saved( $partial_entry, $form ) {

        // Only get data from status 'partial'
 	$partial_entry['status'] = 'partial';

        // Customer Variables assignment
        $email_address = $partial_entry['1'];
	$name = $partial_entry['2'];
	$telephone = $partial_entry['3']; 
}

Now we have all the data from Gravity Forms, how do we send this to our third-party API? For this tutorial, we’re going to CURL in PHP to make the POST. Let’s start by adding $curl = curl_init(); to top of our function.

function send_to_custom_api_on_partial_entry_saved( $partial_entry, $form ) {

        // Only get data from status 'partial'
 	$partial_entry['status'] = 'partial';

        // Initialize CURL
        $curl = curl_init();

        // Customer Variables assignment
        $email_address = $partial_entry['1'];
 
	$name = $partial_entry['2'];
	$telephone = $partial_entry['3']; 
}

We can now add our full CURL array call (we used Postman to build this out).

function send_to_custom_api_on_partial_entry_saved( $partial_entry, $form ) {

        // Only get data from status 'partial'
 	$partial_entry['status'] = 'partial';

        // Initialize CURL
        $curl = curl_init();

        // Customer Variables assignment
        $email_address = $partial_entry['1'];
	$name = $partial_entry['2'];
	$telephone = $partial_entry['3']; 

        curl_setopt_array($curl, array(
                // Your API URL
		CURLOPT_URL => 'https://api.example.com/partial',
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 0,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                // POST Request as we are sending data
		CURLOPT_CUSTOMREQUEST => 'POST',
                // Make sure to get your endpoint fields matched with the correct custom variables
		CURLOPT_POSTFIELDS => 'email_address=' . $email_address . '&name=' . $name . '&telephone=' . $telephone,
		CURLOPT_HTTPHEADER => array(
                        // Update with your API's Content-Type
			'Content-Type: application/x-www-form-urlencoded'
		),
	));
	
	$response = curl_exec($curl);	
	curl_close($curl);
}

Conclusion

Now we have our function built we can finally test! Before uploading the code to your server, it might be worth running a few POST tests on Postman to ensure you’re getting a 200 response along with no errors i.e. data inserted. Once you’re happy with those results you can sync the functions.php file with your production branch and you’re good to go!

Tags:

Gravity Forms PHP

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.