WordPress Return User Data by Remote Request with Bearer Authorization

Thought Industries API User Detail

 

/**
 * RETURN AN ARRAY OF USER DATA FROM REMOTE API
 * {{}} = contents provided by API host/administration, remove brackets when replacing
 * @ int $user_id
 * @ returns array
 */
function get_remote_api_user_data( $user_id ) {
  
  $args = array(  
    'headers' => array( 
      'Authorization' => 'Bearer {{32-CHARACTER ALPHANUMERIC API KEY}}', 
    ),  
    
  ) ;  
  //prevents missed if user emails are stored mixed cases, but email-based endpoint is uniformly lowercase
  $id = strtolower( get_userdata( $user_id )->user_email );  
    
  $response = wp_remote_get( '{{API ENDPOINT URL}}' .  $id, $args  ) ; //get user data   
  $body = wp_remote_retrieve_body( $response ) ;
  $data = json_decode( $body, true ) ; 
    
  return $data ;
  
}

 

 

Leave a Reply