News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Webhook subscription error
Tags: webhooks
Jun 16, 2022 at 8:07am   •   3 replies  •  0 likes
Raymond Labasano

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;

}


on Jun 17, 2022 at 1:13am   •  0 likes

@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


2 Answers
answered on Jun 17, 2022 at 11:17am  

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


 1
answered on Jun 17, 2022 at 4:01am  

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.


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us