4

Is it possible to use wp_remote_post to send http post requests to 3rd party api's? I wasn't able to successfully save the user object as a javascript variable, so I was hoping I could make an http request with php and handle the javascript manipulation in my node express app.

Current attempt:

function identify_user() {
    echo "made it into identify user";
    if( is_user_logged_in()):
        $current_user = wp_get_current_user();
        $user = [];
        $user['id'] =  $current_user->ID;
        $user['user_login'] = $current_user->user_login;
        $user['user_email'] = $current_user->user_email;
        $user['user_firstname'] = $current_user->user_firstname;
        $user['user_lastname'] = $current_user->user_lastname;
        $user['display_name'] = $current_user->display_name;
        $response = wp_remote_post( 'myapp.com/endpoint', array(
           'method' => 'POST',
           'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
           'body' => json_encode($user)
        )
    );

    if ( is_wp_error( $response ) ) {
       $error_message = $response->get_error_message();
       echo "Something went wrong: $error_message";
    } else {
       print_r( $response );
    }
    endif;
}

add_action( 'wp_login', 'identify_user' );

I'm having trouble troubleshooting this code because none of my echo calls are logging to the console. I've seen that you can run error_log(something) but haven't been able to get that working either.

4

The 'body' needs to be an array, not including the 'json_encode($user)' piece.

$response = wp_remote_post( 'myapp.com/endpoint', array(
  'method' => 'POST',
  'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
  'body' => $user
  )
);

I have this in my function since I also had issues with the body being an object:

if (is_object($user) && !is_array($user))
  $user = json_decode(json_encode($user), true);
$body = $user;

Regarding the 'run error_log(something) ...' that you are having problems with, try #David Lee's wrapper function

Basically you can call

write_log('THIS IS THE START OF MY CUSTOM DEBUG');
  // or log data like objects
write_log($object_you_want_to_log);

I don't know what I would do without it.

1

Please try below code might be help to you.

function identify_user() { 
if( is_user_logged_in()): 
$current_user = wp_get_current_user(); 
$_id = $current_user->ID; 
$_email = $current_user->user_email; 
$user = json_encode(array("user_id"=>$_id,"user_email"=>$_email)); 
$curl = curl_init("myeNDPOINT"); 
curl_setopt( $curl, CURLOPT_POST, true ); 
curl_setopt( $curl, CURLOPT_POSTFIELDS,$user); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json')); 
curl_exec( $curl ); 
curl_close( $curl ); 
endif; 
} 

add_action( 'wp_enqueue_scripts', 'identify_user');
0

You can try below code for wp_remote_post.might be you are missing remote post url parameter.

Also i have noticed that you are converting user variable in to json format 2 time which is wrong.

$response = wp_remote_post($url, array(
        'method' => 'POST',
        'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
        'httpversion' => '1.0',
        'sslverify' => false,
        'body' => json_encode($user)
    );
  • I had forgotten to put in a dummy url for example purpoes – Yale Newman Mar 31 '18 at 2:59
  • is wp_remote_post designed for this? or am i better off figuring out curl or ajax. i had an ajax solution but could not get the localize script function working. wordpress.stackexchange.com/questions/299378/… – Yale Newman Mar 31 '18 at 3:02
  • But also you are doing json_encode 2 times. if you already json encoded user before then no need to add json encode under body tag – Somin Mar 31 '18 at 3:03
  • if you use curl then it would be best, – Somin Mar 31 '18 at 3:05
  • i forgot to delete that bit too. i'll edit my question. still nothing happening. no logs on my heroku server and no logs in web browser console. – Yale Newman Mar 31 '18 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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