Skip to content

Instantly share code, notes, and snippets.

@bobwol
Forked from iyut/functions.php
Created June 21, 2023 23:19
Show Gist options
  • Save bobwol/79e3a79a9eea828020569ee124dfbd43 to your computer and use it in GitHub Desktop.
Save bobwol/79e3a79a9eea828020569ee124dfbd43 to your computer and use it in GitHub Desktop.
wp_remote_post using x-www-form-urlencoded
function request_token() {
$encoded_auth = base64_encode( $this->client_id . ":" . $this->client_secret );
$args = array(
'method' => 'POST',
'httpversion' => '1.1',
'headers' => array(
'Authorization' => 'Basic '. $encoded_auth,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
),
'body' => 'grant_type=client_credentials',
);
$response = wp_remote_post( $this->api_url, $args );
// If the status code is not 200, throw an error with the raw response body
if ( isset( $response['response'] ) && $response['response']['code'] !== 200 ) {
throw new RuntimeException( $response['response']['message'] );
}
$token_response = json_decode( $response['body'] );
if( isset( $token_response->error ) ){
throw new RuntimeException( $token_response->error );
}
return $token_response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment