ringcentral/php-sdk
最新稳定版本:3.0.4
Composer 安装命令:
composer require ringcentral/php-sdk
包简介
RingCentral Platform PHP SDK
README 文档
README
RingCentral Developers is a cloud communications platform which can be accessed via more than 70 APIs. The platform's main capabilities include technologies that enable: Voice, SMS/MMS, Fax, Glip Team Messaging, Data and Configurations.
Additional resources
- RingCentral API Reference - an interactive reference for the RingCentral API that allows developers to make API calls with no code.
- Document - an interactive reference for the SDK code documentation.
Requirements
- PHP 7.2+
- CURL extension
- MCrypt extension
Installation
Please choose one of the following installation options:
With Composer (recommended)
The installation of composer is local by default. We suggest that you install it at the top level of your application's directory structure.
-
Install composer:
$ curl -sS https://getcomposer.org/installer | phpMore info about installation on Linux / Unix / OSX and Windows.
-
Run the Composer command to install the latest version of SDK:
$ php composer.phar require ringcentral/ringcentral-php
-
Require Composer's autoloader in your PHP script (assuming it is in the same directory where you installed Composer):
require('vendor/autoload.php');
PHAR with bundled dependencies
This is not recommended! Use Composer as modern way of working with PHP packages.
-
Download PHAR file
-
Require files:
require('path-to-sdk/ringcentral.phar');
Please keep in mind that bundled dependencies may interfere with your other dependencies.
Basic Usage
Initialization
$rcsdk = new RingCentral\SDK\SDK('clientId', 'clientSecret', RingCentral\SDK\SDK::SERVER_PRODUCTION);
You also may supply custom AppName and AppVersion parameters with your application codename and version. These parameters are optional but they will help a lot to identify your application in API logs and speed up any potential troubleshooting. Allowed characters for AppName and AppVersion are: letters, digits, hyphen, dot and underscore.
$rcsdk = new RingCentral\SDK\SDK('clientId', 'clientSecret', RingCentral\SDK\SDK::SERVER_PRODUCTION, 'MyApp', '1.0.0');
For production use RingCentral\SDK\SDK::SERVER_PRODUCTION constant. Or type in the server URL by hand.
Authentication
Check authentication status:
$rcsdk->platform()->loggedIn();
Authenticate user with jwt:
$rcsdk->platform()->login([ 'jwt' => 'your_jwt_token' ]);
Authenticate user with authorization code:
$rcsdk->platform()->login([ 'code' => 'authorization code from RingCentral login redirect uri' ]);
Authentication lifecycle
Platform class performs token refresh procedure if needed. You can save authentication between requests in CGI mode:
// when application is going to be stopped file_put_contents($file, json_encode($rcsdk->platform()->auth()->data(), JSON_PRETTY_PRINT)); // and then next time during application bootstrap before any authentication checks: $rcsdk->platform()->auth()->setData(json_decode(file_get_contents($file), true));
Important! You have to manually maintain synchronization of SDK's between requests if you share authentication. When two simultaneous requests will perform refresh, only one will succeed. One of the solutions would be to have semaphor and pause other pending requests while one of them is performing refresh.
Performing API call
$apiResponse = $rcsdk->platform()->get('/account/~/extension/~'); $apiResponse = $rcsdk->platform()->post('/account/~/extension/~', array(...)); $apiResponse = $rcsdk->platform()->put('/account/~/extension/~', array(...)); $apiResponse = $rcsdk->platform()->delete('/account/~/extension/~'); print_r($apiResponse->json()); // stdClass will be returned or exception if Content-Type is not JSON print_r($apiResponse->request()); // PSR-7's RequestInterface compatible instance used to perform HTTP request print_r($apiResponse->response()); // PSR-7's ResponseInterface compatible instance used as HTTP response
Multipart response
Loading of multiple comma-separated IDs will result in HTTP 207 with Content-Type: multipart/mixed. This response will
be parsed into multiple sub-responses:
$presences = $rcsdk->platform() ->get('/account/~/extension/id1,id2/presence') ->multipart(); print 'Presence loaded ' . $presences[0]->json()->presenceStatus . ', ' . $presences[1]->json()->presenceStatus . PHP_EOL;
Send SMS - Make POST request
$apiResponse = $rcsdk->platform()->post('/account/~/extension/~/sms', array( 'from' => array('phoneNumber' => 'your-ringcentral-sms-number'), 'to' => array( array('phoneNumber' => 'mobile-number'), ), 'text' => 'Test from PHP', ));
Get Platform error message
try { $rcsdk->platform()->get('/account/~/whatever'); } catch (\RingCentral\SDK\Http\ApiException $e) { // Getting error messages using PHP native interface print 'Expected HTTP Error: ' . $e->getMessage() . PHP_EOL; // In order to get Request and Response used to perform transaction: $apiResponse = $e->apiResponse(); print_r($apiResponse->request()); print_r($apiResponse->response()); // Another way to get message, but keep in mind, that there could be no response if request has failed completely print ' Message: ' . $e->apiResponse->response()->error() . PHP_EOL; }
How to debug HTTP
You can set up any HTTPS sniffer (e.g. proxy server, like Charles) and route SDK traffic to it by providing a custom Guzzle Client instance:
use GuzzleHttp\Client as GuzzleClient; $guzzle = new GuzzleClient([ 'proxy' => 'localhost:8888', 'verify' => false ]); $rcsdk = new SDK("clientId", "clientSecret", SDK::SERVER_PRODUCTION, 'Demo', '1.0.0', $guzzle);
Subscriptions
Webhook Subscriptions
$apiResponse = $rcsdk->platform()->post('/subscription', array( 'eventFilters' => array( '/restapi/v1.0/account/~/extension/~/message-store', '/restapi/v1.0/account/~/extension/~/presence' ), 'deliveryMode' => array( 'transportType' => 'WebHook', 'address' => 'https://consumer-host.example.com/consumer/path' ) ));
When webhook subscription is created, it will send a request with validation-token in headers to webhook address. Webhook address should return a success request with validation-token in headers to finish webhook register.
WebSocket Subscriptions
use RingCentral\SDK\WebSocket\WebSocket; use RingCentral\SDK\WebSocket\Subscription; use RingCentral\SDK\WebSocket\Events\NotificationEvent; // connect websocket $websocket = $rcsdk->initWebSocket(); $websocket->addListener(WebSocket::EVENT_READY, function (SuccessEvent $e) { print 'Websocket Ready' . PHP_EOL; print 'Connection Details' . print_r($e->apiResponse()->body(), true) . PHP_EOL; }); $websocket->addListener(WebSocket::EVENT_ERROR, function (ErrorEvent $e) { print 'Websocket Error' . PHP_EOL; }); $websocket->connect(); // create subscription $subscription = $rcsdk->createSubscription(); $subscription->addEvents(array( '/restapi/v1.0/account/~/extension/~/presence', '/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS' )); $subscription->addListener(Subscription::EVENT_NOTIFICATION, function (NotificationEvent $e) { print 'Notification ' . print_r($e->payload(), true) . PHP_EOL; }); $subscription->register();
We need to create websocket connection before creating subscription. When websocket connection get error, need to re-created websocket and subscription manually.
PubNub Subscriptions
This is deprecated, please use WebSocket Subscription.
use RingCentral\SDK\Subscription\Events\NotificationEvent; use RingCentral\SDK\Subscription\PubnubSubscription; $subscription = $rcsdk->createSubscription('Pubnub); $subscription->addEvents(array('/restapi/v1.0/account/~/extension/~/presence')) $subscription->addListener(PubnubSubscription::EVENT_NOTIFICATION, function (NotificationEvent $e) { print_r($e->payload()); }); $subscription->setKeepPolling(true); $apiResponse = $subscription->register();
Please keep in mind that due to limitations of the PubNub library, which is synchronous, subscriptions may expire and must be re-created manually.
Multipart Requests
SDK provides a helper to make sending of faxes easier.
$request = $rcsdk->createMultipartBuilder() ->setBody(array( 'to' => array( array('phoneNumber' => '16501112233'), ), 'faxResolution' => 'High', )) ->add('Plain Text', 'file.txt') ->add(fopen('path/to/file', 'r')) ->request('/account/~/extension/~/fax'); // also has optional $method argument $response = $rcsdk->platform()->sendRequest($request);
How to demo?
Clone the repo and create a file demo/_credentials.php copy the contents from the file 'demo/_credentialsSample.php' as shown below:
return array( 'username' => '18881112233', // your RingCentral account phone number 'extension' => null, // or number 'password' => 'yourPassword', 'clientId' => 'yourClientId', 'clientSecret' => 'yourClientSecret', 'server' => 'https://platform.ringcentral.com', // for production - https://platform.ringcentral.com 'smsNumber' => '18882223344', // any of SMS-enabled numbers on your RingCentral account 'mobileNumber' => '16501112233', // your own mobile number to which script will send sms 'dateFrom' => 'yyyy-mm-dd', 'dateTo' => 'yyyy-mm-dd' );
Then execute:
$ php index.php
Should output:
Auth exception: Refresh token has expired
Authorized
Refreshing
Refreshed
Users loaded 10
Presence loaded Something New - Available, Something New - Available
Expected HTTP Error: Not Found (from backend)
SMS Phone Number: 12223334455
Sent SMS https://platform.ringcentral.com/restapi/v1.0/account/111/extension/222/message-store/333
Subscribing
After that script will wait for any presence notification. Make a call to your account or make outbound call from your account. When you will make a call, script will print notification and exit.
Please take a look in demo folder to see all the demos.
ringcentral/php-sdk 适用场景与选型建议
ringcentral/php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 103.85k 次下载、GitHub Stars 达 54, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「connect」 「sdk」 「platform」 「ringcentral」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ringcentral/php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ringcentral/php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ringcentral/php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Customize JWT claims in Laravel Passport access tokens
A PSR-7 compatible library for making CRUD API endpoints
Official PHP SDK for Okta Connect — the omnichannel messaging platform (WhatsApp, email, social publishing, campaigns).
The SNS Connect Module provide social features using their social network accounts.
Laravel model support multi connection format master - slaves. Connect random a slave with weight
Alfabank REST API integration
统计信息
- 总下载量: 103.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 54
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知