Send user data to remote API on login using PHP CURL and wp_login

With wp_enqueue_scripts:

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(“myWebsite.com/endpoint”);
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’);

This is not ideal as will fire on every page where a user is logged in.

With wp_login, I’ve gotten this logging $user_obj, but now the CURL request isn’t working.

function identify_user( $user_login, $user ) {
$_id = $user->ID;
$_email = $user->user_email;
$user_obj = json_encode(array(“user_id”=>$_id,”user_email”=>$_email));
$curl = curl_init(“https://www.mywebsite.com/endpoint”);
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS,$user_obj);
curl_setopt($curl, CURLOPT_HTTPHEADER,array(‘Content-Type:application/json’));
curl_exec( $curl );
curl_close( $curl );
}

add_action( ‘wp_login’, ‘identify_user’, 10, 2);

Read more here:: Send user data to remote API on login using PHP CURL and wp_login

Leave a Reply

Your email address will not be published. Required fields are marked *