blenderdeluxe / ortc-php
ORTC (RealTime.co 实时 pub/sub 框架) 的 PHP API 封装器
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
- ramsey/uuid: ^3.4
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.0
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 密钥。
-
创建新的订阅
-
您可以看到您的
应用程序密钥
和私钥
-
如果您想使用身份验证,您应该在面板中启用它。
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)
- 支持存在频道
- 还有其他什么吗?!
贡献
想要贡献?只需分叉此项目并提交 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.
*/