eventflit / eventflit-http-php
用于与Eventflit REST API交互的库
Requires
- php: ^5.4 || ^7.0
- ext-curl: *
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.7
This package is not auto-updated.
Last update: 2024-10-03 04:45:42 UTC
README
PHP库,用于与Eventflit HTTP API交互。
在 https://eventflit.com 注册并使用以下方式在你的应用程序中添加应用程序凭证。
安装
您可以通过名为 eventflit-http-php
的composer包获取Eventflit PHP库。请参阅 https://packagist.org.cn/packages/eventflit/eventflit-http-php
$ composer require eventflit/eventflit-http-php
或在 composer.json
中添加以下内容:
"require": { "eventflit/eventflit-http-php": "^0.1" }
然后运行 composer update
。
或者您可以克隆或下载库文件。
我们建议您使用 composer。
此库依赖于PHP的cURL和JSON模块。请参阅 cURL模块安装说明 和 JSON模块安装说明。
Eventflit构造函数
使用您的Eventflit应用程序的凭据创建一个新的Eventflit\Eventflit
实例。
$app_id = 'YOUR_APP_ID'; $app_key = 'YOUR_APP_KEY'; $app_secret = 'YOUR_APP_SECRET'; $app_cluster = 'YOUR_APP_CLUSTER'; $eventflit = new Eventflit\Eventflit( $app_key, $app_secret, $app_id, array('cluster' => $app_cluster) );
第四个参数是一个$options
数组。附加选项包括:
scheme
- 例如 http 或 httpshost
- 主机,例如 service.eventflit.com。不要以反斜杠结尾。port
- HTTP端口timeout
- HTTP超时encrypted
- 快速选项,使用https方案和443端口。cluster
- 指定应用程序运行所在的集群。curl_options
- 包含自定义curl命令的数组
例如,默认情况下将通过非加密连接进行调用。要将其更改为使用HTTPS进行调用,请使用以下命令:
$eventflit = new Eventflit\Eventflit( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true ) );
例如,如果您想设置自定义curl选项,请使用以下命令
$eventflit = new Eventflit\Eventflit( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true, 'curl_options' => array( CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4 ) ) );
注意:host
选项会覆盖cluster
选项!
发布/触发事件
要在一个或多个通道上触发事件,请使用trigger
函数。
单个通道
$eventflit->trigger( 'my-channel', 'my_event', 'hello world' );
多个通道
$eventflit->trigger( [ 'channel-1', 'channel-2' ], 'my_event', 'hello world' );
批量
您还可以通过单个API调用发送多个事件(在多租户集群上每个调用最多10个事件)
$batch = array(); $batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('hello' => 'world')); $batch[] = array('channel' => 'my-channel', 'name' => 'my_event', 'data' => array('myname' => 'bob')); $eventflit->triggerBatch($batch);
数组
对象将自动转换为JSON格式
$array['name'] = 'joe'; $array['message_count'] = 23; $eventflit->trigger('my_channel', 'my_event', $array);
输出将是
"{'name': 'joe', 'message_count': 23}"
套接字ID
为了避免重复,您可以在触发事件时可选地指定发送者的套接字ID(https://eventflit.com/docs/duplicates)
$eventflit->trigger('my-channel','event','data','socket_id');
JSON格式
如果您的数据已经是JSON格式编码的,您可以通过设置第六个参数为true来避免第二次编码步骤,如下所示
$eventflit->trigger('my-channel', 'event', 'data', null, false, true)
认证私有通道
要授权您的用户访问Eventflit上的私有通道,您可以使用socket_auth函数
$eventflit->socket_auth('private-my-channel','socket_id');
认证存在通道
使用存在通道与私有通道类似,但您可以指定额外数据来识别特定用户
$eventflit->presence_auth('presence-my-channel','socket_id', 'user_id', 'user_info');
存在示例
首先在您的JS应用程序中设置此变量
Eventflit.channel_auth_endpoint = '/presence_auth.php';
然后,在presence_auth.php中创建以下内容
<?php if (isset($_SESSION['user_id'])) { $stmt = $pdo->prepare("SELECT * FROM `users` WHERE id = :id"); $stmt->bindValue(':id', $_SESSION['user_id'], PDO::PARAM_INT); $stmt->execute(); $user = $stmt->fetch(); } else { die('aaargh, no-one is logged in'); } header('Content-Type: application/json'); $eventflit = new Eventflit\Eventflit($key, $secret, $app_id); $presence_data = array('name' => $user['name']); echo $eventflit->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);
注意:此假设您将用户存储在名为users
的表中,并且那些用户有一个name
列。它还假设您有一个登录机制,该机制将登录用户的user_id
存储在会话中。
应用程序状态查询
获取有关通道的信息
$eventflit->get_channel_info( $name );
也可以从Eventflit REST API获取有关频道的详细信息。
$info = $eventflit->get_channel_info('channel-name'); $channel_occupied = $info->occupied;
对于存在频道,您还可以查询当前订阅此频道的不重复用户数量(单个用户可以多次订阅,但只计算一次)
$info = $eventflit->get_channel_info('presence-channel-name', array('info' => 'user_count')); $user_count = $info->user_count;
如果您已启用查询subscription_count
(当前订阅此频道连接的数量)的功能,则可以按以下方式查询此值
$info = $eventflit->get_channel_info('presence-channel-name', array('info' => 'subscription_count')); $subscription_count = $info->subscription_count;
获取应用程序频道的列表
$eventflit->get_channels()
您也可以从Eventflit REST API获取应用程序频道的列表。
$result = $eventflit->get_channels(); $channel_count = count($result->channels); // $channels is an Array
获取筛选后的应用程序频道列表
$eventflit->get_channels( array( 'filter_by_prefix' => 'some_filter' ) )
您也可以根据名称前缀获取频道列表。为此,您需要向调用提供$选项参数。以下示例中,调用将返回所有以'presence-'为前缀的频道的列表。这对于获取所有存在频道的列表非常理想。
$results = $eventflit->get_channels( array( 'filter_by_prefix' => 'presence-') ); $channel_count = count($result->channels); // $channels is an Array
这也可以使用通用的eventflit->get
函数实现
$eventflit->get( '/channels', array( 'filter_by_prefix' => 'presence-' ) );
从存在频道获取用户信息
$response = $eventflit->get( '/channels/presence-channel-name/users' )
$response
的格式如下
Array ( [body] => {"users":[{"id":"a_user_id"}]} [status] => 200 [result] => Array ( [users] => Array ( [0] => Array ( [id] => a_user_id ) /* Additional users */ ) ) )
通用获取函数
$eventflit->get( $path, $params );
用于对Eventflit REST API进行GET
查询。处理身份验证。
响应是一个具有result
索引的关联数组。此索引的内容取决于调用的REST方法。然而,始终存在一个允许HTTP状态码的status
属性,如果状态码表示API调用成功,则设置result
属性。
$response = $eventflit->get( '/channels' ); $http_status_code = $response[ 'status' ]; $result = $response[ 'result' ];
推送通知(BETA版)
Eventflit现在允许向iOS和Android设备发送本地通知。请查看文档,了解如何在Android和iOS上设置推送通知。无需使用此库进行任何额外设置。它可以直接与Eventflit实例一起使用。您只需要相同的Eventflit凭据。
本地通知API托管在push.eventflit.com
,并且只监听HTTPS。如果您想提供不同的主机,可以这样做
$eventflit = new Eventflit\Eventflit($app_key, $app_secret, $app_id, array('notification_host' => 'custom notifications host'))
然而,请注意,默认的notification_host
是push.eventflit.com
,并且它是唯一支持端点。
发送本地推送
您可以使用notify
方法发送本地通知。该方法接受两个参数
interests
:一个字符串数组,表示您的设备订阅的兴趣。兴趣类似于DDN中的频道。目前,您最多可以向十个兴趣发布通知。data
:这表示您希望作为通知的一部分发送的有效负载。您可以根据您想向哪个平台发送通知提供键值对的关联数组。您必须包含gcm
或apns
键。有关可接受键的详细列表,请参阅iOS和Android的文档。
它还像trigger
方法一样接受一个debug
参数,以便进行调试。
示例
$data = array( 'apns' => array( 'aps' => array( 'alert' => array( 'body' => 'tada' ), ), ), 'gcm' => array( 'notification' => array( 'title' => 'title', 'icon' => 'icon' ), ), ); $eventflit->notify(array("test"), $data);
错误
推送通知请求一旦提交到服务,就会异步执行。为了使错误报告更容易,您可以在请求体中提供webhook_url
字段。服务将调用此URL,并在其中包含发布请求的结果。
以下是一个示例
$data = array( 'apns' => array("..."), 'gcm' => array("..."), 'webhook_url' => "http://my.company.com/eventflit/nativepush/results" ); $eventflit->notify(array("test"), $data);
调试与日志记录
调试您应用程序与服务器交互的最好方法是设置库的记录器,这样您就可以查看库内部工作原理以及与Eventflit服务的交互。
PSR-3 支持
日志记录的推荐方法是使用一个符合PSR-3规范的记录器,实现Psr\Log\LoggerInterface
接口。`Eventflit`对象实现了Psr\Log\LoggerAwareInterface
接口,这意味着您需要调用setLogger(LoggerInterface $logger)
来设置记录器实例。
// where $logger implements `LoggerInterface` $eventflit->setLogger($logger);
自定义记录器(已弃用)
警告:现在使用
Eventflit::set_logger()
和自定义的实现了log()
的对象已被弃用,并将在未来移除。请使用PSR-3兼容的记录器。
您通过将一个具有log
函数的对象传递给eventflit->set_logger
函数来设置日志记录。
class MyLogger { public function log( $msg ) { print_r( $msg . "\n" ); } } $eventflit->set_logger( new MyLogger() );
如果您在控制台/终端中执行上述示例代码,调试信息将输出到那里。如果您在一个Web应用程序中使用它,输出将出现在生成的应用程序输出中,例如HTML。
运行测试
需要phpunit。
- 运行
composer install
- 转到
test
目录 - 重命名
config.example.php
,并使用有效的Eventflit凭据替换其中的值,或者创建环境变量。 - 一些测试需要客户端连接到您在配置中定义的应用程序;您可以通过在浏览器中打开https://panel.eventflit.com/app#/app/<YOUR_TEST_APP_ID>/#/getting-started
- 从项目的根目录执行
composer exec phpunit
以运行所有测试。
框架集成
许可证
版权所有 2017,Eventflit。许可协议为MIT:https://open-source.org.cn/licenses/mit-license.php
版权所有 2014,Pusher。许可协议为MIT:https://open-source.org.cn/licenses/mit-license.php
版权所有 2010,Squeeks。许可协议为MIT:https://open-source.org.cn/licenses/mit-license.php