Created
May 20, 2019 06:59
-
-
Save martinleblanc/07ca8addcd2dfa8f2b99d0b92e2acc9c to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Send a POST requst using cURL | |
* @param string $CLIENT_ID the client ID | |
* @param string $CLIENT_SECRET the client secret | |
* @return string | |
*/ | |
function get_token($CLIENT_ID, $CLIENT_SECRET, $AUTH_URL) { | |
// Request token | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $AUTH_URL); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=jwt_bearer&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
// Prepare response | |
header('Content-type: application/json'); | |
// return JWT to requesting app | |
if($result == FALSE) { | |
// Something went wrong. | |
echo json_encode(array("error" => "CURL request failed.")); | |
return false; | |
} | |
else { | |
$result_decoded = json_decode($result); | |
if(array_key_exists("error", $result_decoded) or !array_key_exists("access_token", $result_decoded)) { | |
// Use this for additional error handling. | |
return $result; | |
} | |
else { | |
// Print successful response. | |
return $result; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment