Last active
March 5, 2021 14:21
-
-
Save twentyfortysix/22bab5e7715bc590ae1f3836b59f5c2b to your computer and use it in GitHub Desktop.
heds up.. wp_remote_post, the Wordpress way
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
<?php | |
function get_POI_token($POI_URL_BASE, $POI_URL_TOKEN, $POI_PASS, $POI_USER, $POI_ID, $POI_SECRET){ | |
// do your houseworks.. | |
$url = $POI_URL_BASE.$POI_URL_TOKEN; | |
$username = $POI_USER; | |
$password = $POI_PASS; | |
$client_id = $POI_ID; | |
$client_secret = $POI_SECRET; | |
// some wanted url encoded | |
$args = array( | |
'headers' => array( | |
'Content-Type' => 'application/x-www-form-urlencoded', | |
'Cache-Control' => 'no-cache' | |
), | |
"body" => "grant_type=password&client_id={$client_id}&client_secret={$client_secret}&username={$username}&password={$password}" | |
// some likes it as json | |
// 'headers' => array( | |
// 'Content-Type' => 'application/json', | |
// ), | |
// 'data' => json_encode( | |
// array( | |
// "grant_type" => "password", | |
// 'password' => $password, | |
// 'username' => $username, | |
// 'client_id' => $client_id, | |
// 'client_secret' => $client_secret | |
// ) | |
// ) | |
); | |
$response = wp_remote_post($url, $args); | |
if($response['response']['code'] == 200){ | |
$body = json_decode(wp_remote_retrieve_body($response)); | |
$payload = array( | |
'token' => $body->access_token, | |
'expires_in' => $body->expires_in | |
); | |
return $payload; | |
}else{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment