定制 jithvar/yii2-ringcentral 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jithvar/yii2-ringcentral

Composer 安装命令:

composer require jithvar/yii2-ringcentral

包简介

Send Fax using RingCentral with Yii2

README 文档

README

This extension provides RingCentral Fax integration for Yii2 framework with OAuth 2.0 support.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist jithvar/yii2-ringcentral

or add

"jithvar/yii2-ringcentral": "*"

to the require section of your composer.json file.

Configuration

Basic Configuration

'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']);
        }
    ],
]

OAuth 2.0 Setup

  1. Go to RingCentral Developer Portal
  2. Select your application
  3. Under "Auth & Security":
    • Enable "OAuth 2.0"
    • Enable "Issue refresh tokens"
    • Add your redirect URI (e.g., https://your-app.com/ringcentral/callback)

Implementing OAuth Flow

  1. Create a controller to handle the OAuth flow:
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']);
        }
    }
}
  1. Add routes in config/web.php:
'urlManager' => [
    'enablePrettyUrl' => true,
    'rules' => [
        'ringcentral/auth' => 'ring-central/auth',
        'ringcentral/callback' => 'ring-central/callback',
    ],
],
  1. Add a link to start the OAuth flow:
use yii\helpers\Html;

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

Token Management

The extension handles token management automatically:

  1. When tokens are first obtained via OAuth:

    • Both access and refresh tokens are saved via your tokenRefreshCallback
    • The tokens are used for subsequent API calls
  2. When the access token expires:

    • The extension automatically uses the refresh token to get a new access token
    • Your tokenRefreshCallback is called with the new tokens
    • The failed request is automatically retried
  3. If the refresh token expires:

    • The user will need to re-authenticate via OAuth
    • You can catch this case by checking for the 'refresh_token_expired' error

Usage

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());
}

Best Practices

  1. Store sensitive credentials securely:
'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'),
],
  1. Use environment-specific URLs:
'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',
  1. Always implement the tokenRefreshCallback to persist new tokens:
'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();
},

License

This project is licensed under the MIT License - see the LICENSE file for details.

jithvar/yii2-ringcentral 适用场景与选型建议

jithvar/yii2-ringcentral 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「extension」 「yii2」 「fax」 「ringcentral」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jithvar/yii2-ringcentral 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jithvar/yii2-ringcentral 我们能提供哪些服务?
定制开发 / 二次开发

基于 jithvar/yii2-ringcentral 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-10