Created
June 24, 2021 05:05
-
-
Save iyut/d1cbe71b681cc7869e1f46eeb609ffc5 to your computer and use it in GitHub Desktop.
wp_remote_post using x-www-form-urlencoded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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