phpwordpressgravityforms

Gravity Forms - gform_after_submission - Unauthorized


I'm running into a strange challenge using gform_after_submission with Gravity Forms on wordpress.

PHP isn't my main programming language so I'm doing a lot of trial and error to try and get this working.

Below is my code in the themes Functions.php file

    add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
function post_to_third_party( $entry, $form ) {
 
    $endpoint_url = '[Correct API Endpoint]';
    $body = array(
        'prospectFirstName' => $entry['18'],
//         'prospectLastName' => $entry['19'],
//         'prospectEmail' => $entry['2'],
//         'prospectPhone' => $entry['5'],
//         'notes' => $entry['3'],
        'source' => 'Website',
        'communityId' => 32
        );
    
    $args = array(
        'headers' => [
            'accept' => 'text/plain',
            'Authorization' => 'Basic ' . base64_encode( 'Correct-username:Correct-password' ),
            'Content-Type' => 'application/json'
        ],
        'method' => 'POST',
        'body' => $body
    );  
    
    GFCommon::log_debug( 'gform_after_submission: JSON Encode => ' . json_encode($args, JSON_UNESCAPED_SLASHES) );
 
    $response = wp_remote_get( $endpoint_url, json_encode($args) );
//     $response = wp_remote_post( $endpoint_url, json_encode($args) );
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( json_encode($response), true ) );
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );

I've tried the following for the $response call

$response = wp_remote_get( $endpoint_url, json_encode($args) );

$response = wp_remote_get( $endpoint_url, json_encode($args, JSON_UNESCAPED_SLASHES) );

$response = wp_remote_get( $endpoint_url, $args);

When I don't json_encode I get an error "errors":{"$":["'p' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]

I would really appreciate any help. I'm able to use these same settings using Postman and CF7 to Any API plugin

Thank you


Solution

  • I have made some of modifications to the code so it can work correctly, could you please try with the given one.

    <?php
    add_action( 'gform_after_submission', 'post_to_third_party', 10, 2 );
    
    function post_to_third_party( $entry, $form ) {
        $endpoint_url = '[Correct API Endpoint]'; // This is to set API endpoint here.
        $body         = array(
            'prospectFirstName' => rgar($entry, '18'), // We are using rgar for safer access.
            'source' => 'Website',
            'communityId' => 32
        );
    
        $args = array(
            'headers' => [
                'accept'        => 'application/json', // We are using application/json if the API accepts JSON.
                'Authorization' => 'Basic ' . base64_encode('Correct-username:Correct-password'),
                'Content-Type'  => 'application/json'
            ],
            'body'    => json_encode($body), // This is to JSON encode the body.
            'timeout' => 45, // This si to increase timeout if needed.
        );
    
        GFCommon::log_debug('gform_after_submission: Sending to API => ' . print_r($args, true));
    
        // We are using wp_remote_post for sending data.
        $response = wp_remote_post($endpoint_url, $args);
    
        // This is to check for errors in the response.
        if ( is_wp_error( $response ) ) {
            GFCommon::log_error('gform_after_submission: Error sending data => ' . $response->get_error_message());
        } else {
            GFCommon::log_debug('gform_after_submission: API response => ' . print_r($response, true));
        }
    }