2

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
  • And what result do you get? What will you see if you log $request variable at the end of this function? Feb 17, 2019 at 17:32

2 Answers 2

3

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 );
}
3
  • Thank you sMyles I have now managed to get error logging going which means I can debug better. It still isn't working but it is at least printing the response which I can pull apart. It seems that trying to create the 'party' is preventing the submission. Feb 18, 2019 at 18:37
  • I have managed to get the data pushed through if I pass absolute data. Now that I am attempting to retrieve and send dynamic data my error log is populating with 'Unable to get userdata!' so the if statement is obviously failing. Do I need to do anything other than what is in the below plugin code in order to retrieve the $user object data after a new user is created **Ive updated the original code block in the question but it is being peer reviewed Feb 19, 2019 at 17:11
  • @DanSutherland you can try changing get_userdata to get_user_by( 'id', $user_id ); (as get_userdata is just a wrapper) ... but it is also a pluggable function, so depending on what you have on your site it is possible something is overriding that fn call --- if you're seeing Unable to get userdata that means that function is returning false instead of user data
    – sMyles
    Feb 19, 2019 at 22:08
0

Can you try this? This should be working:

$args = array(
    'headers' => array(
            'Authorization' => 'Bearer {private token goes here!!!!}',
            'Content-Type' => 'application/json'
        ),
   'body' => json_encode($body)
);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.