Send data to 3rd party api with wp_remote_post on wp_login

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.

Leave a Comment