pubnub / pubnub
这是官方的 PubNub PHP SDK 仓库。
Requires
- php: ^7.4|>=8.0
- psr/log: ^1.1|^2.0|^3.0
- rmccue/requests: ^2.0
Requires (Dev)
- monolog/monolog: ^2.9 || ^3.0
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- dev-master
- 7.0.1
- 7.0.0
- 6.3.0
- 6.2.1
- 6.2.0
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.1
- 6.0.0
- 5.1.0
- 5.0.0
- 4.7.0
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.7
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- 4.0.0-beta.3
- 4.0.0-beta.2
- 4.0.0-beta
- 4.0.0-alpha
- 3.8.3
- 3.8.1
- 3.8.0
- v3.7.9
- v3.7.8
- v3.7.7
- v3.7.6
- v3.7.5
- v3.7.4
- v3.7.3
- v3.7.2
- v3.7.1
- v3.7.0
- v3.6.3
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.4
- v3.5.3
- v3.5.2
- 3.5
- dev-fetat/message-persistence
- dev-chore/update-compatibility-info
- dev-feat/crypto_module
- dev-ghatest
- dev-feature/add-space-id-and-message-type
- dev-feature/GrantToken-behat-tests
- dev-feature/access-management-revoke-token
- dev-metadata-changes
- dev-composer-json-fix
- dev-develop
- dev-JeffGreen7-sourceclr
- dev-edge
- dev-master_3x
This package is auto-updated.
Last update: 2024-09-10 13:09:42 UTC
README
这是官方的 PubNub PHP SDK 仓库。
PubNub 负责您应用程序实时通信层所需的基础设施和 API。专注于您应用程序的逻辑,让 PubNub 在 100 毫秒内处理全球范围内的数据发送和接收。
该 SDK 支持 PHP 7.4 和 8.x。
获取密钥
您需要发布和订阅密钥来验证您的应用程序。从管理员门户获取您的密钥。
配置 PubNub
-
将 PHP SDK 集成到您的项目中
-
不使用 composer
-
克隆以下仓库:
git clone https://github.com/pubnub/php.git ./pubnub-php
-
将
src
文件夹复制到您的项目中。 -
在您的项目中包含
autoloader.php
文件require_once('src/autoloader.php');
-
从https://github.com/Seldaek/monolog 下载依赖项
monolog
,并将monolog
文件夹从src
文件夹复制到您的项目的src
文件夹中。 -
从https://github.com/php-fig/log/tree/master 下载依赖项
psr/Log
,并将psr
文件夹复制到您的项目的src
文件夹中。 -
从https://github.com/WordPress/Requests 下载依赖项
rmccue
,并将Requests
文件夹和Requests.php
文件从library
文件夹复制到您的项目的src
文件夹中。
-
-
使用 composer
-
将 PubNub 包添加到您的
composer.json
文件中{ "require": { <!-- include the latest version from the badge at the top --> "pubnub/pubnub": "7.0.1" } }
-
从命令行运行
composer install --no-dev
。这将 PubNub PHP SDK 及其所有依赖项安装到项目的vendor
文件夹中。 -
在您的项目中包含
autoload.php
文件require_once('vendor/autoload.php');
-
-
-
配置您的密钥
$pnconf = new PNConfiguration(); $pubnub = new PubNub($pnconf); $pnconf->setSubscribeKey("mySubscribeKey"); $pnconf->setPublishKey("myPublishKey"); $pnconf->setUserId("ReplaceWithYourClientIdentifier");
添加事件监听器
class MySubscribeCallback extends SubscribeCallback { function status($pubnub, $status) { if ($status->getCategory() === PNStatusCategory::PNUnexpectedDisconnectCategory) { // This event happens when radio / connectivity is lost } else if ($status->getCategory() === PNStatusCategory::PNConnectedCategory){ // Connect event. You can do stuff like publish, and know you'll get it // Or just use the connected event to confirm you are subscribed for // UI / internal notifications, etc } else if ($status->getCategory() === PNStatusCategory::PNDecryptionErrorCategory){ // Handle message decryption error. Probably client configured to // encrypt messages and on live data feed it received plain text. } } function message($pubnub, $message){ // Handle new message stored in message.message } function presence($pubnub, $presence){ // handle incoming presence data } } $subscribeCallback = new MySubscribeCallback(); $pubnub->addListener($subscribeCallback);
发布/订阅
$pubnub->subscribe() ->channels("hello_world") ->execute(); $pubnub->publish() ->channel("hello_world") ->message("Hello PubNub!") ->sync();
文档
支持
如果您 需要帮助 或有 一般问题,请联系 support@pubnub.com。