Skip to main content

Need help on this PHP. If someone can fix this for me please.


https://drive.google.com/drive/folders/1Zswnlp4qwMGM31numcbDKoDZRjXm-uSx?usp=sharing


<?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";

$headers[] = "Accept: application/json";

$headers[] = "Authorization: Basic " . base64_encode(CLIENT_ID . ":" . APP_SECRET);

$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);

$subscription_id = $response['id'];

$response = readPresence($token);

print_r($response);

$response = getSubscriptionid($token, $subscription_id);

print_r($response);

$response = getSubscription($token);

print_r($response);

$response = renewSubscription($token, $subscription_id);

print_r($response);

mysql_close();

function getSubscriptionid($token, $id)
{
$url = BASE_URL . "/restapi/v1.0/subscription/$id";

$headers = array();

$headers[] = "Content-Type: application/json";

$headers[] = "Authorization: Bearer " . $token;

$headers[] = "Accept: application/json";

$response = curlGet($url, $headers);

return $response;

}

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/safeforu/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/$id/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;

}


@Raymond Labasano

I can see you created 2 issues for the exact same question.
- Request you to stick to only asking one question to avoid duplicates.
I am closing one of the issues for the same reason
- Please use the code styling options when sharing code so that it is properly indented and legible. I am fixing it for you for now
- Please ask directed questions. The above post does not specify if you are facing any errors and what is the error message you see



Hi thank you for you reply. The response I get is subscription limit exceeded. Can this be push to production manually? I know we cant change the permissions once its graduated and thats fine. Those are the only permissions we need.


Hi - To whom it may concern. Please close this. There’s a dev support case already for this matter. Thank you.


Reply