<?php
define("BASE_URL","https://platform.ringcentral.com");
//define("BASE_URL","https://platform.devtest.ringcentral.com");
define("CLIENT_ID","xXXXXXxxxXXXXXXXXx");
define("APP_SECRET","dddddddXXXXXXXXxxxxxxxXXXXXXXXXXxxxxxxxxXXXXXX");
define("RED_URI","https://tbmslive.com/ringcentral/platinum/process.php");
<?php
require_once("config.php");
if(file_exists("token.json") && file_get_contents("token.json") != ""){
$tok_time = file_get_contents("token_expiry.txt");
$ref_time = file_get_contents("refresh_token_expiry.txt");
if(time() > $tok_time && time() > $ref_time){
unlink("token.json");
unlink("token_expiry.txt");
unlink("refresh_token_expiry.txt");
$url = BASE_URL."/restapi/oauth/authorize?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".RED_URI;
header("Location: ".$url);
exit();
}else if(time() > $tok_time && time() < $ref_time){
$response = json_decode(file_get_contents("token.json"), true);
$refresh_token = $response['refresh_token'];
$response = get_Access_token_by_refresh_token($refresh_token);
$token = $response['access_token'];
file_put_contents("token.json", json_encode($response));
file_put_contents("token_expiry.txt", time() + $response['expires_in']);
file_put_contents("refresh_token_expiry.txt", time() + $response['refresh_token_expires_in']);
}else{
$response = json_decode(file_get_contents("token.json"), true);
$token = $response['access_token'];
}
}else{
if(isset($_GET['code']) && $_GET['code'] != ""){
$code = $_GET['code'];
$url = BASE_URL."/restapi/oauth/token";
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$data = array(
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" => RED_URI,
"client_id"=> CLIENT_ID
);
$response = curlPost($url, $headers, $data, false);
$token = $response['access_token'];
file_put_contents("token.json", json_encode($response));
file_put_contents("token_expiry.txt", time() + $response['expires_in']);
file_put_contents("refresh_token_expiry.txt", time() + $response['refresh_token_expires_in']);
$sub = 2 * 24 * 60 * 60;
$expiry = $response['refresh_token_expires_in'] - $sub;
file_put_contents("expiry.txt", time() + $expiry);
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER["PHP_SELF"];
header("Location: ".$url);
}else{
$url = BASE_URL."/restapi/oauth/authorize?response_type=code&client_id=".CLIENT_ID."&redirect_uri=".RED_URI;
header("Location: ".$url);
exit();
}
}
echo "<pre>";
//$response = subscribe($token);
//print_r($response);
$response = getSubscription($token);
print_r($response);
mysql_close();
function curlPost($url, $header, $data, $type){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
if($type){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch);
curl_close ($ch);
$responce = json_decode($result,true);
return($responce);
}
function curlGet($url, $header){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch);
curl_close ($ch);
$responce = json_decode($result,true);
return($responce);
}
function get_Access_token_by_refresh_token($get_refresh_token){
$url = BASE_URL."/restapi/oauth/token";
$headers = array();
$auth = base64_encode(CLIENT_ID .":". APP_SECRET);
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Authorization: Basic ".$auth;
$data = array(
"grant_type" => "refresh_token",
"refresh_token" => $get_refresh_token
);
$response = curlPost($url, $headers, $data, false);
return $response;
}
/*
Get Token
*/
function subscribe($token){
$url= BASE_URL."/restapi/v1.0/subscription";
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$token;
$headers[] = "Accept: application/json";
$data = array("eventFilters"=>array("/restapi/v1.0/account/~/presence?detailedTelephonyState=true"), "deliveryMode"=>array( "transportType"=> "WebHook","address"=>"https://tbmslive.com/ringcentral/platinum/webhook.php"));
$response = curlPost($url, $headers, json_encode($data), true);
return $response;
}
function readPresence($token){
$url= BASE_URL."/restapi/v1.0/account/~/extension/~/presence";
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$token;
$headers[] = "Accept: application/json";
$response = curlGet($url, $headers);
return $response;
}
function getSubscription($token){
$url= BASE_URL."/restapi/v1.0/subscription";
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$token;
$headers[] = "Accept: application/json";
$response = curlGet($url, $headers);
return $response;
}
function renewSubscription($token, $id){
$url= BASE_URL."/restapi/v1.0/subscription/{subscriptionId}/renew";
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer ".$token;
$headers[] = "Accept: application/json";
$data = array();
$response = curlPost($url, $headers, json_encode($data), true);
return $response;
}