$json = getToken($code, $access_token_url, $client_id, $client_secret, $callback_url);
function getToken($code, $access_token_url, $client_id, $client_secret, $callback_url){
$curl = curl_init();
$url = $access_token_url."?"
."code=".$code
."&grant_type=authorization_code"
."&client_id=". $client_id
."&redirect_uri=". $callback_url;
printf("<p> url: %s (without client secret) </p>\n",$url);
$url .= '&client_secret='. $client_secret;
$params = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
),
);
curl_setopt_array($curl, $params);
printf("<p> executing curl POST to url");
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
printf("<p> response (a): \n");
print_r($response);
printf("</p>\n");
printf("<p>json return: %s </p>\n",$response);
if ($err) {
echo "cURL Error #01: " . $err;
} else {
$array_response = json_decode($response, true);
printf("<p>response: %s </p>\n",print_r($array_response,true));
if(array_key_exists("access_token", $array_response)) return $array_response;
if(array_key_exists("error", $array_response)) echo $array_response["error_description"];
echo "<p>cURL Error #02: Something went wrong! Please contact admin.</p>";
}
}