Stuck with wp_remote_post sending data to an external api on user registration

I am trying to create a basic plugin to send user details to a CRM when a user signs up on the WordPress website as a basic subscriber.

I have reached a sticking point, I have tried to interpret the API details as best as I can, but I am unsure, if I am formatting my body data properly as it just isn’t working.

I can get the POST to work using Postman, however that is via json/javascript and on-site I am trying to achieve the same result using wp_remote_post.

Any pointers or observations would be appreciated.

add_action( 'user_register', 'send_new_user', 10, 1 );

function send_new_user($user_id) {

    $new_user = get_userdata($user_id);
    $useremail = $new_user -> user_email;

    $url="https://api.capsulecrm.com/api/v2/parties";
    $body = array(
        'firstName' => 'WhyWontYouWork',
        'email' => $useremail
    );

    $args = array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'sslverify' => false,
        'blocking' => false,
        'headers' => array(
            'Authorization' => 'Bearer {private token goes here!!!!}',
            'Content-Type' => 'application/json',
            'Accept' => 'application/json',
        ),
        'body' => json_encode($body),
        'cookies' => array()
    );

    $request = wp_remote_post ($url, $args);
};

1 Answer
1

I recommend only setting the arguments you absolutely need to change from defaults, to eliminate potential issues while testing. It’s best to only add additional arguments as they are needed.

All arguments can be found here:
https://developer.wordpress.org/reference/classes/WP_Http/request/

If possible also remove the required authentication at first to just test the POST before adding in security/authentication.

You also don’t need to close a function with a semicolon ;

I recommend looking up how to setup xdebug so you can debug your code locally, if you can’t do that, then I recommend using error_log to log to your hosting providers error log, so you can see what the response is.

You also set blocking to false, do you not want a response from the server?

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
    die;
}

//Find the details of a new user

add_action('user_register', 'send_doqaru_user', 10, 1);

function send_doqaru_user ($user_id){

    $new_doqaru_user = get_userdata($user_id);
    $doqaru_user_email = $new_doqaru_user -> user_email;

    // get all the meta data of the newly registered user
    $new_user_data = get_user_meta($user_id);

    // get the first name of the user as a string
    $doqaru_user_firstname = get_user_meta( $user_id, 'first_name', true );
    $doqaru_user_lastname = get_user_meta( $user_id, 'last_name', true );


    if( ! $new_user ){
        error_log( 'Unable to get userdata!' );
        return;
    }

    $url="https://api.capsulecrm.com/api/v2/parties";
    $body = array(
        'type' => 'person',
        'firstName' => $doqaru_user_firstname,
        'lastName' => $doqaru_user_lastname,
        'emailAddresses'     => array(
            'type' => 'Work',
            'address' => $new_user->user_email

    ));

    $args = array(
        'method'      => 'POST',
        'timeout'     => 45,
        'sslverify'   => false,
        'headers'     => array(
            'Authorization' => 'Bearer {token goes here}',
            'Content-Type'  => 'application/json',
        ),
        'body'        => json_encode($body),
    );

    $request = wp_remote_post( $url, $args );

    if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
        error_log( print_r( $request, true ) );
    }

    $response = wp_remote_retrieve_body( $request );
}

Leave a Comment