macromindonline / pusher-php-server
用于与 Pusher REST API 交互的库
Requires
- php: ^5.4 || ^7.0
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: ^4.8 || ^5.7
This package is not auto-updated.
Last update: 2024-09-18 02:54:10 UTC
README
PHP 库,用于与 Pusher HTTP API 交互。
在 https://pusher.com 上注册并使用以下方式在您的应用程序中使用应用程序凭据。
安装
您可以通过名为 pusher-php-server 的 composer 包获取 Pusher PHP 库。请参阅 https://packagist.org.cn/packages/pusher/pusher-php-server
$ composer require pusher/pusher-php-server
或添加到 composer.json
"require": { "pusher/pusher-php-server": "^3.0" }
然后运行 composer update。
或者您可以直接克隆或下载库文件。
我们建议您使用 composer。
此库依赖于 PHP 的 cURL 和 JSON 模块。请参阅 cURL 模块安装说明 和 JSON 模块安装说明。
Pusher 构造函数
使用您的 Pusher 应用程序的凭据创建一个新的 Pusher\Pusher 实例。
$app_id = 'YOUR_APP_ID'; $app_key = 'YOUR_APP_KEY'; $app_secret = 'YOUR_APP_SECRET'; $app_cluster = 'YOUR_APP_CLUSTER'; $pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array('cluster' => $app_cluster) );
第四个参数是一个 $options 数组。额外的选项包括:
scheme- 例如 http 或 httpshost- 主机,例如 api.pusherapp.com。不要以反斜杠结尾。port- HTTP 端口timeout- HTTP 超时encrypted- 快速选项,用于使用 https 方案和 443 端口。cluster- 指定应用程序运行所在的集群。curl_options- 自定义 cURL 命令的数组
例如,默认情况下将通过非加密连接调用。要将此更改为使用 HTTPS 进行调用,请使用
$pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true ) );
例如,如果您想设置自定义 cURL 选项,请使用此方法
$pusher = new Pusher\Pusher( $app_key, $app_secret, $app_id, array( 'cluster' => $app_cluster, 'encrypted' => true, 'curl_options' => array( CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4 ) ) );
注意:此库中的 $options 参数是在 2.2.0 版本中引入的。以前可以为每个选项传递额外的参数,但这变得难以管理。然而,保持了向后兼容性。
注意:host 选项会覆盖 cluster 选项!
发布/触发事件
要在一个或多个频道上触发事件,请使用 trigger 函数。
单个频道
$pusher->trigger( 'my-channel', 'my_event', 'hello world' );
多个频道
$pusher->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')); $pusher->triggerBatch($batch);
数组
对象将自动转换为 JSON 格式
$array['name'] = 'joe'; $array['message_count'] = 23; $pusher->trigger('my_channel', 'my_event', $array);
输出将是
"{'name': 'joe', 'message_count': 23}"
套接字 ID
为了避免重复,您可以在触发事件时可选地指定发送者的套接字 ID (https://pusher.com/docs/duplicates)
$pusher->trigger('my-channel','event','data','socket_id');
JSON 格式
如果您的数据已经是 JSON 格式,您可以通过将第六个参数设置为 true 来避免第二次编码步骤,如下所示
$pusher->trigger('my-channel', 'event', 'data', null, false, true)
验证私有频道
要授权您的用户访问 Pusher 上的私有频道,您可以使用 socket_auth 函数
$pusher->socket_auth('private-my-channel','socket_id');
验证存在频道
使用存在频道类似于私有频道,但您可以指定额外的数据来标识特定用户
$pusher->presence_auth('presence-my-channel','socket_id', 'user_id', 'user_info');
存在示例
首先在您的 JS 应用程序中设置此变量
Pusher.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'); $pusher = new Pusher\Pusher($key, $secret, $app_id); $presence_data = array('name' => $user['name']); echo $pusher->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);
注意:此假设您将用户存储在名为 users 的表中,并且这些用户有一个 name 列。它还假设您有一个登录机制,该机制将已登录用户的 user_id 存储在会话中。
应用程序状态查询
获取有关频道的详细信息
$pusher->get_channel_info( $name );
您还可以从 Pusher REST API 获取有关频道的详细信息。
$info = $pusher->get_channel_info('channel-name'); $channel_occupied = $info->occupied;
对于 存在频道,您还可以查询当前订阅此频道的不同用户数量(单个用户可能多次订阅,但只计算一次)
$info = $pusher->get_channel_info('presence-channel-name', array('info' => 'user_count')); $user_count = $info->user_count;
如果您已启用查询 subscription_count(当前订阅此频道的连接数)的功能,则可以按以下方式查询此值
$info = $pusher->get_channel_info('presence-channel-name', array('info' => 'subscription_count')); $subscription_count = $info->subscription_count;
获取应用程序频道的列表
$pusher->get_channels()
您还可以从 Pusher REST API 获取应用程序的频道列表。
$result = $pusher->get_channels(); $channel_count = count($result->channels); // $channels is an Array
获取过滤后的应用程序频道列表
$pusher->get_channels( array( 'filter_by_prefix' => 'some_filter' ) )
您还可以根据名称前缀获取频道列表。为此,您需要在调用中提供 $options 参数。在以下示例中,调用将返回所有以 'presence-' 为前缀的频道列表。这对于检索所有存在频道非常有用。
$results = $pusher->get_channels( array( 'filter_by_prefix' => 'presence-') ); $channel_count = count($result->channels); // $channels is an Array
这也可以使用通用的 pusher->get 函数实现
$pusher->get( '/channels', array( 'filter_by_prefix' => 'presence-' ) );
从存在频道获取用户信息
$response = $pusher->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 */ ) ) )
通用获取函数
$pusher->get( $path, $params );
用于对 Pusher REST API 执行 GET 查询。处理身份验证。
响应是一个具有 result 索引的关联数组。此索引的内容取决于调用的 REST 方法。然而,始终存在一个表示 HTTP 状态码的 status 属性,如果状态码表示对 API 的成功调用,则将设置 result 属性。
$response = $pusher->get( '/channels' ); $http_status_code = $response[ 'status' ]; $result = $response[ 'result' ];
推送通知(测试版)
Pusher 现在允许向 iOS 和 Android 设备发送原生通知。请参阅文档了解如何在 Android 和 iOS 上设置推送通知。使用此库无需额外设置。它与相同的 Pusher 实例一起开箱即用。您只需要相同的 Pusher 凭据。
原生通知 API 帐户位于 nativepush-cluster1.pusher.com,并且仅监听 HTTPS。如果您希望提供不同的主机,您可以这样做
$pusher = new Pusher\Pusher($app_key, $app_secret, $app_id, array('notification_host' => 'custom notifications host'))
但是请注意,默认情况下 notification_host 为 nativepush-cluster1.pusher.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' ), ), ); $pusher->notify(array("test"), $data);
错误
一旦将推送通知请求提交到服务,就会异步执行。为了使报告错误更容易,您可以在请求正文中提供 webhook_url 字段。该服务将调用此 URL 并将包含发布请求结果的正文。
以下是一个示例
$data = array( 'apns' => array("..."), 'gcm' => array("..."), 'webhook_url' => "http://my.company.com/pusher/nativepush/results" ); $pusher->notify(array("test"), $data);
调试与日志记录
调试您的应用程序与服务器交互的最佳方法是为库设置一个日志记录器,这样您可以看到库内部的运行情况以及与Pusher服务的交互。
您可以通过将一个包含log函数的对象传递给pusher->set_logger函数来设置日志记录。
class MyLogger { public function log( $msg ) { print_r( $msg . "\n" ); } } $pusher->set_logger( new MyLogger() );
如果您在控制台/终端中执行上述示例代码,调试信息将输出到那里。如果您在Web应用程序中使用它,输出将出现在生成的应用程序输出中,例如HTML。
运行测试
需要phpunit。
- 进入
test目录 - 重命名
config.example.php并将值替换为有效的Pusher凭据,或者创建环境变量。 - 某些测试需要客户端连接到在配置中定义的应用程序;您可以通过在浏览器中打开https://dashboard.pusher.com/apps/<YOUR_TEST_APP_ID>/console来完成此操作
- 从项目根目录执行
phpunit .来运行所有测试。
框架集成
- Laravel 4 - https://github.com/artdarek/pusherer
- Laravel 5 - https://github.com/vinkla/Laravel-Pusher
许可证
版权所有2014年,Pusher。根据MIT许可证授权:https://open-source.org.cn/licenses/mit-license.php
版权所有2010年,Squeeks。根据MIT许可证授权:https://open-source.org.cn/licenses/mit-license.php