gform_webhooks_request_headers

Description

The “gform_webhooks_request_headers” filter allows the webhook HTTP request headers to be modified.

Usage

The following would apply to all forms:

add_filter( 'gform_webhooks_request_headers' 'your_function_name', 10, 4 );

To target a specific form, append the form id to the hook name. (format: gform_webhooks_request_headers_FORMID)

add_filter( 'gform_webhooks_request_headers_14' 'your_function_name', 10, 4 );

Parameters

Examples

1. Set the From header

add_filter( 'gform_webhooks_request_headers', 'set_headers', 10, 4 );
function set_headers( $request_headers, $feed, $entry, $form ){
	$request_headers['From'] = '[email protected]';
	return $request_headers;
}

2. Set the authorization header #1

This example shows how you can generate and add a bearer token to the authorization header. In this case the token is being generated by a third-party service (goabroadhq.com) and then cached locally until it expires.

add_filter( 'gform_webhooks_request_headers', function ( $request_headers ) {
	$token = GFCache::get( 'goabroadhq_token' );

	if ( ! $token ) {
		$post_url = 'https://hq-api.goabroadhq.com/token';
		$body     = array(
			'client_id'     => 'something',
			'client_secret' => 'something',
			'grant_type'    => 'client_credentials',
		);
		$headers  = array( 'Content-Type' => 'application/x-url-form-encoded' );

		$response = wp_remote_post( $post_url, array( 'body' => $body, 'headers' => $headers ) );
		gf_webhooks()->log_debug( 'gform_webhooks_request_headers: response => ' . print_r( $response, true ) );

		if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) {
			$response_body_json  = wp_remote_retrieve_body( $response );
			$response_body_array = json_decode( $response_body_json, true );

			if ( isset( $response_body_array['access_token'] ) ) {
				$token = $response_body_array['access_token'];
				GFCache::set( 'goabroadhq_token', $token, true, $response_body_array['expires_in'] - 30 );
			}
		}
	}

	if ( $token ) {
		gf_webhooks()->log_debug( 'gform_webhooks_request_headers: token added.' );
		$request_headers['Authorization'] = 'Bearer ' . $token;
	}

	return $request_headers;
} );

3. Set the authorization header #2

This example shows how you can generate and add a bearer token to the authorization header. In this case the token is being generated by the JWT PHP Library for use with the Zoom API.

add_filter( 'gform_webhooks_request_headers', function ( $request_headers ) {
	// Requires JWT PHP Library https://github.com/firebase/php-jwt
	if ( class_exists( 'JWT' ) ) {
		// Zoom API credentials from https://developer.zoom.us/me/
		$key    = '<zoom_api_key>';
		$secret = '<zoom_api_secret>';
		$token  = array(
			"iss" => $key,
			// The benefit of JWT is expiry tokens, we'll set this one to expire in 1 minute
			"exp" => time() + 60
		);

		$request_headers['Authorization'] = 'Bearer ' . JWT::encode( $token, $secret );
		gf_webhooks()->log_debug( 'gform_webhooks_request_headers: token added.' );
	}

	return $request_headers;
} );

4. Set the authorization header #3

This example shows how you can add the Basic Authorization header to authenticate a request.

add_filter( 'gform_webhooks_request_headers', function ( $request_headers ) {
	$request_headers['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );

	return $request_headers;
}, 10, 2 );

Note: The above example is based on USERNAME and PASSWORD being constants defined elsewhere (i.e. the wp-config.php file), if not, you can include the values within the quotes e.g. ‘USERNAME:PASSWORD’.

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GF_Webhooks::process_feed() in gravityformswebhooks/class-gf-webhooks.php.