PHP code example of jithvar / yii2-ringcentral

1. Go to this page and download the library: Download jithvar/yii2-ringcentral library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jithvar / yii2-ringcentral example snippets


'components' => [
    'ringcentralFax' => [
        'class' => 'ringcentral\fax\RingCentralFax',
        'clientId' => 'YOUR_CLIENT_ID',
        'clientSecret' => 'YOUR_CLIENT_SECRET',
        'serverUrl' => 'https://platform.ringcentral.com', // Use 'https://platform.devtest.ringcentral.com' for sandbox
        'redirectUrl' => 'https://your-app.com/ringcentral/callback',
        'tokenRefreshCallback' => function($tokens) {
            // Save new tokens to your storage
            Yii::$app->cache->set('ringcentral_access_token', $tokens['access_token']);
            Yii::$app->cache->set('ringcentral_refresh_token', $tokens['refresh_token']);
        }
    ],
]

namespace app\controllers;

use Yii;
use yii\web\Controller;

class RingCentralController extends Controller
{
    /**
     * Initiates OAuth flow
     */
    public function actionAuth()
    {
        // Generate a random state for CSRF protection
        $state = Yii::$app->security->generateRandomString();
        Yii::$app->session->set('ringcentral_state', $state);

        // Get authorization URL and redirect
        $authUrl = Yii::$app->ringcentralFax->getAuthorizationUrl($state);
        return $this->redirect($authUrl);
    }

    /**
     * Handles OAuth callback
     */
    public function actionCallback()
    {
        // Verify state parameter
        $state = Yii::$app->request->get('state');
        $savedState = Yii::$app->session->get('ringcentral_state');
        
        if (!$state || $state !== $savedState) {
            throw new \yii\web\BadRequestHttpException('Invalid state parameter');
        }

        // Exchange authorization code for tokens
        $code = Yii::$app->request->get('code');
        try {
            $tokens = Yii::$app->ringcentralFax->handleOAuthCallback($code);
            
            // Tokens are automatically saved via tokenRefreshCallback
            Yii::$app->session->setFlash('success', 'Successfully connected to RingCentral');
            return $this->redirect(['site/index']);
            
        } catch (\Exception $e) {
            Yii::$app->session->setFlash('error', 'Failed to connect to RingCentral: ' . $e->getMessage());
            return $this->redirect(['site/index']);
        }
    }
}

'urlManager' => [
    'enablePrettyUrl' => true,
    'rules' => [
        'ringcentral/auth' => 'ring-central/auth',
        'ringcentral/callback' => 'ring-central/callback',
    ],
],

use yii\helpers\Html;

echo Html::a('Connect RingCentral', ['ring-central/auth'], ['class' => 'btn btn-primary']);

try {
    // Send a fax
    $result = Yii::$app->ringcentralFax->send([
        'to' => '+1234567890',
        'files' => ['/path/to/file.pdf'],
        'text' => 'Optional cover page text'
    ]);
} catch (\yii\base\Exception $e) {
    if (strpos($e->getMessage(), 'refresh_token_expired') !== false) {
        // Redirect user to re-authenticate
        return $this->redirect(['ring-central/auth']);
    }
    // Handle other errors
    Yii::error('Fax sending failed: ' . $e->getMessage());
}

'ringcentralFax' => [
    'class' => 'ringcentral\fax\RingCentralFax',
    'clientId' => getenv('RINGCENTRAL_CLIENT_ID'),
    'clientSecret' => getenv('RINGCENTRAL_CLIENT_SECRET'),
    'redirectUrl' => getenv('RINGCENTRAL_REDIRECT_URL'),
    'serverUrl' => getenv('RINGCENTRAL_SERVER_URL'),
],

'serverUrl' => YII_DEBUG 
    ? 'https://platform.devtest.ringcentral.com' 
    : 'https://platform.ringcentral.com',
'redirectUrl' => YII_DEBUG
    ? 'http://localhost:8080/ringcentral/callback'
    : 'https://your-app.com/ringcentral/callback',

'tokenRefreshCallback' => function($tokens) {
    // Save to database
    Yii::$app->db->createCommand()
        ->update('settings', [
            'access_token' => $tokens['access_token'],
            'refresh_token' => $tokens['refresh_token']
        ], ['name' => 'ringcentral'])
        ->execute();
},