为 ChatNio 提供的PHP平台SDK
- 通过Composer 安装(1)
创建一个composer.json
文件,然后将以下代码添加到composer.json
中:
{
"require": {
"hujiayucc/chatnio": "^x.x.x"
}
}
然后在命令行中执行以下命令:
composer install # 安装
composer update # 更新
- 通过Composer 安装(2)
composer require hujiayucc/chatnio # 安装
composer update # 更新
- 通过Git 安装
git clone https://github.com/hujiayucc/ChatNio-SDK-PHP.git
cd ChatNio-SDK-PHP
composer install # 安装
composer update # 更新
- 使用
在开始使用 SDK 之前,你需要首先生成一个ChatNio
实例,这需要提供一个由ChatNio
服务提供的密钥:
require_once 'vendor/autoload.php';
use com\hujiayucc\chatnio\ChatNio;
$key = "你的密钥";
$chatNio = new ChatNio(key);
$package = $chatNio->Pets()->Package();
echo "\ncert: " . ($package->isCert() ? "true" : "false");
echo "\nTeenager: " . ($package->isTeenager() ? "true" : "false");
$subscribe = $chatNio->Subscribe();
echo "\n" . "isSubscribed: " . ($subscribe->isSubscribed() ? "true" : "false");
echo "\n" . "expired: " . $subscribe->expired();
$buy = $subscribe->buy(1, new SubLevel(SubLevel::Standard));
echo "\n" . ($buy ? "buy success" : "buy failed");
通过调用以下方法购买余额:
echo "\n" . ($chatNio->Pets()->buy(1) ? "true" : "false");
SDK 提供了一个便捷的对话查询方法:
$tasks = $chatNio->Tasks();
$taskList = $tasks->getTaskList();
foreach ($taskList as $task) {
echo "\n" . $task->__toString();
}
这将返回一个TaskBean
对象的列表,你可以通过这些TaskBean
对象获取例如任务的id
、userId
、name
、model
、enableWeb
等信息,并且获取任务的Message
消息列表。
你可以使用SDK提供的方法查询订阅状态,购买订阅和续费等操作
$package = $chatNio->Pets()->Package();
echo "\ncert: " . ($package->isCert() ? "true" : "false");
echo "\nTeenager: " . ($package->isTeenager() ? "true" : "false");
使用 SDK,你可以选择同步方式或异步方式进行消息发送。其中,Token
对象用于指定用户或密钥和对话id,也可以设置为匿名或新对话。
$async = new ChatAsync(new Token($key));
$async->sendMessage("写一段PHP调用WebSocket的示例");
echo "\n" . "async send success: " . $async->getMessages();
$sync = new ChatSync(new Token($key), new class extends CustomSync
{
function onMessage(MessageSegment $message)
{
echo "\n" . $message->getMessage();
}
function onError(Exception $exception)
{
throw new FiledException($exception->getMessage());
}
});
$sync->sendMessage("写一段PHP调用WebSocket的示例");
echo "\n" . "sync send success: " . $sync->getMessages();
在异步方式下,你可以重写 onMessage
方法来接收消息,也可以重写 onError
方法来处理错误。
<?php
require_once "vendor/autoload.php";
// 下面两个可以删掉,只是方便导入秘钥
global $key;
include "config.php";
use com\hujiayucc\chatnio\bean\MessageSegment;
use com\hujiayucc\chatnio\bean\Token;
use com\hujiayucc\chatnio\ChatNio;
use com\hujiayucc\chatnio\data\ChatAsync;
use com\hujiayucc\chatnio\data\ChatSync;
use com\hujiayucc\chatnio\enums\SubLevel;
use com\hujiayucc\chatnio\exception\AuthException;
use com\hujiayucc\chatnio\exception\BuyException;
use com\hujiayucc\chatnio\exception\FiledException;
use com\hujiayucc\chatnio\utils\CustomSync;
$chatNio = new ChatNio($key);
try {
echo $chatNio->Pets()->getQuota();
} catch (AuthException|FiledException $e) {
echo("\n" . $e->getMessage());
}
try {
echo "\n" . ($chatNio->Pets()->buy(1) ? "true" : "false");
echo "\n" . $chatNio->Pets()->getQuota();
} catch (AuthException|BuyException|FiledException $e) {
echo("\n" . $e->getMessage());
}
try {
$package = $chatNio->Pets()->Package();
echo "\ncert: " . ($package->isCert() ? "true" : "false");
echo "\nTeenager: " . ($package->isTeenager() ? "true" : "false");
} catch (FiledException|AuthException $e) {
echo("\n" . $e->getMessage());
}
try {
$tasks = $chatNio->Tasks();
$taskList = $tasks->getTaskList();
foreach ($taskList as $task) {
echo "\n" . $task->__toString();
}
} catch (AuthException|FiledException $e) {
echo("\n" . $e->getMessage());
}
try {
$task = $tasks->getTask(3);
echo "\n" . $task->__toString();
} catch (AuthException|FiledException $e) {
echo("\n" . $e->getMessage());
}
try {
$delete = $tasks->deleteTask(1);
echo "\n" . ($delete ? "delete success" : "delete failed");
} catch (AuthException|FiledException $e) {
echo("\n" . $e->getMessage());
}
$subscribe = $chatNio->Subscribe();
try {
echo "\n" . "isSubscribed: " . ($subscribe->isSubscribed() ? "true" : "false");
echo "\n" . "expired: " . $subscribe->expired();
$buy = $subscribe->buy(1, new SubLevel(SubLevel::Standard));
echo "\n" . ($buy ? "buy success" : "buy failed");
} catch (AuthException|FiledException|BuyException $e) {
echo("\n" . $e->getMessage());
}
// 同步调用
$async = new ChatAsync(new Token($key));
$async->sendMessage("你好");
echo "\n" . "async send success: " . $async->getMessages() . "\n";
// 异步调用
$sync = new ChatSync(new Token($key), new class extends CustomSync
{
function onMessage(MessageSegment $message)
{
echo $message->getMessage();
}
function onError(Exception $exception)
{
throw new FiledException($exception->getMessage());
}
});
$sync->sendMessage("写一段PHP调用WebSocket的示例");
echo "\n" . "sync send success: " . $sync->getMessages();