nikapps / ortc-php
ORTC (来自 RealTime.co 的实时 pub/sub 框架) 的 PHP API 封装
Requires
- php: >=5.5.9
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- phpunit/phpunit: 4.8
This package is not auto-updated.
Last update: 2024-09-14 17:42:40 UTC
README
这是 ORTC (Open Real-Time Connectivity,来自 realtime.co 的 PHP 5.4+ 实时云 pub/sub 框架) 的非官方客户端,但功能强大,基于 composer 且兼容 psr-1、psr-2 和 psr-4。
安装
使用 composer,通过运行以下命令安装此 包
composer require nikapps/ortc-php
配置
获取应用程序密钥和私有密钥
首先,您应该在 realtime.co 上注册并获取您的 API 密钥。
-
在 https://accounts.realtime.co 登录/注册
-
创建新的订阅
-
您可以看到您的
应用程序密钥
和私有密钥
-
如果您想使用身份验证,您应该在面板中启用它。
Ortc 配置
在您可以调用任何 API 调用之前,您应该设置您的凭据。
$ortcConfig = new \Nikapps\OrtcPhp\Configs\OrtcConfig(); $ortcConfig->setApplicationKey('YOUR_APPLICATION_KEY'); //you application key $ortcConfig->setPrivateKey('YOUR_PRIVATE_KEY'); //Your private key $ortcConfig->setVerifySsl(true); //verify ssl/tls certificate
完成!
用法
获取均衡器 URL (手动)
此包会自动获取均衡器 URL (最佳可用服务器),但如果你想手动获取新的均衡器 URL
$ortc = new \Nikapps\OrtcPhp\Ortc($ortcConfig); $balancerUrl = $ortc->getBalancerUrl(); echo 'Balancer Url: ' . $balancerUrl->getUrl();
身份验证
为了对用户进行身份验证
- 创建通道
首先,您应该创建您的通道
$channelOne = new \Nikapps\OrtcPhp\Models\Channel(); $channelOne->setName('CHANNEL_ONE_NAME'); $ChannelOne->setPermission(Channel::PERMISSION_WRITE); $channelTwo = new \Nikapps\OrtcPhp\Models\Channel(); $channelTwo->setName('CHANNEL_TWO_NAME'); $channelTwo->setPermission(Channel::PERMISSION_READ); $channels = [ $channelOne, $channelTwo ];
- 身份验证
然后对用户进行身份验证
$authToken = 'YOUR_AUTH_TOKEN'; //your authentication token $authRequest = new \Nikapps\OrtcPhp\Models\Requests\AuthRequest(); $authRequest->setAuthToken($authToken); $authRequest->setExpireTime(5 * 60); //token ttl (expiration time) in seconds $authRequest->setPrivate(true); //Indicates whether the authentication token is private $authRequest->setChannels($channels); $ortc = new \Nikapps\OrtcPhp\Ortc($ortcConfig); $ortc->authenticate($authRequest);
发送消息 (推送)
为了将消息推送到通道
$authToken = 'YOUR_AUTH_TOKEN'; //your authentication token $sendMessageRequest = new \Nikapps\OrtcPhp\Models\Requests\SendMessageRequest(); $sendMessageRequest->setAuthToken($authToken); $sendMessageRequest->setChannelName('CHANNEL_NAME'); $sendMessageRequest->setMessage('YOUR_MESSAGE'); $ortc = new \Nikapps\OrtcPhp\Ortc($ortcConfig); $ortc->sendMessage($sendMessageRequest);
如果您使用的是 UTF-8 消息,最好使用 base64_encode()
。
异常
- OrtcException
其他异常的父类
- UnauthorizedException
当令牌过期或凭据无效时
- InvalidBalancerUrlException
当均衡器 URL 无效时
-您可以通过 getUrl()
获取 URL
- BatchRequestException
当至少一条消息响应失败时
-您可以通过 getResults()
获取所有结果。它返回一个 \GuzzleHttp\BatchResults
对象。
- NetworkErrorException
Guzzle ClientExcpetion
-您可以通过 getClientException()
获取 guzzle 异常
依赖
Ortc 文档
此包基于 ORTC REST API。您可以从此 URL 下载 REST 服务文档
http://messaging-public.realtime.co/documentation/rest/2.1.0/RestServices.pdf
此外,您还可以从此 URL 下载官方 ORTC PHP 库
http://messaging-public.realtime.co/api/download/php/2.1.0/ApiPhp.zip
框架集成
- Laravel 4/5: nikapps/ortc-laravel
待办事项
添加单元测试 (codeception 或 phpunit)(感谢 @moura137)- 通过 Ratchet/Nodejs/Icicle/Amphp 订阅通道
- 支持移动推送通知 (iOS & Android)
- 支持存在通道
- 还有其他什么吗?!
贡献
想贡献吗?只需 fork 此项目并创建一个 pull request!
许可
本项目采用 MIT 许可协议 发布。
/*
* Copyright (C) 2015 NikApps Team.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* 1- The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/