Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
Created October 10, 2021 19:55
Show Gist options
  • Select an option

  • Save stevesohcot/2d9c3653b07369740241f20b28e4c687 to your computer and use it in GitHub Desktop.

Select an option

Save stevesohcot/2d9c3653b07369740241f20b28e4c687 to your computer and use it in GitHub Desktop.
PHP Ring Central 3 Legged Auth Login - 1 of 2
<?php
require('vendor/autoload.php');
use RingCentral\SDK\Http\HttpException;
use RingCentral\SDK\Http\ApiResponse;
use RingCentral\SDK\SDK;
$rcsdk = new SDK(constant('RINGCENTRAL_CLIENT_ID'), constant('RINGCENTRAL_CLIENT_SECRET'), constant('RINGCENTRAL_SERVER_URL'));
$platform = $rcsdk->platform();
// Using the authUrl to call the platform function
$url = $platform->authUrl(array(
'redirectUri' => constant('RINGCENTRAL_REDIRECT_URL'), // something like "engine.php?oauth2callback"
'state' => 'initialState',
'brandId' => '',
'display' => '',
'prompt' => ''
));
function ringCentralCallGetRequest($endpoint, $params){
global $platform;
try {
$resp = $platform->get($endpoint, $params);
#echo "<p>".$resp->text()."</p>";
return $resp->text();
} catch (\RingCentral\SDK\Http\ApiException $e) {
print 'Expected HTTP Error: ' . $e->getMessage() . PHP_EOL;
}
}
?>
<?php if (!isset($_SESSION['ringCentralSessionAccessToken'])) { ?>
<a href="<?php echo $url; ?>">Login RingCentral Account</a>
<?php
} else {
$platform->auth()->setData((array)$_SESSION['ringCentralSessionAccessToken']);
if (!$platform->loggedIn()) {
?>
<p>Invalid login, please try again:</p>
<a href="<?php echo $url; ?>">Login RingCentral Account</a>
<?php
} else {
?>
<h1>Ring Central Call Logs</h1>
<?php
$queryParams = array();
$queryParams['perPage'] = 999;
$endpoint = "/restapi/v1.0/account/~/call-log";
$data = json_decode( ringCentralCallGetRequest($endpoint, $queryParams) );
print "<pre>";
print_r( $data);
print "</pre>";
$records = $data->records;
if (count($records) == 0) {
print "No data available";
} else {
// print / loop through the data
}
} // logged in
} // session set
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment