WordPress Put Request to Remote API

Thought Industries API Create and Update User
/**
 * SET SUBSCRIPTION ACCESS AT REMOTE API
 * Utilizes "PUT" method 
 * {{}} = contents provided by API host/administration, remove brackets when replacing
 * @ int $user_id
 * @ string $course_slug
 */
function set_subscription_at_remote_api( $user_id, $course_slug ) { 

  $user_email = get_userdata( $user_id )->user_email ;
  
  $data = wp_json_encode( array(  
    'email' => $user_email, 
    'bundleSlugs' => array( $course_slug ), 
    )
   ); 
  
  $id = $user_email ;
  $args = array( 
    'method' => 'PUT',
    'headers' => array( 
      'Authorization' => 'Bearer {{32-CHARACTER ALPHANUMERIC KEY}}',  
      'Content-Type' => 'application/json',  //JSON is default but no harm in specifying
    ),	
    'body' => $data,
  ) ; 
    
    
  //make request and return response to check results
  $response = wp_remote_request( '{{URL PROVIDED BY API HOST}}' .  $id, $args  ) ; 
  $body = wp_remote_retrieve_body( $response ) ;
  $data = json_decode( $body, true ) ;
  
  $subscription_id = $data[purchasedBundles][0]['bundleId'] ;
  $subscription_slug = get_sub_slug_by_bundle_id( $subscription_id ) ;
  
  return $subscription_slug ; 
  
}

 

 

Leave a Reply