PHP code example of hujiayucc / chatnio
1. Go to this page and download the library: Download hujiayucc/chatnio library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hujiayucc / chatnio example snippets
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");
$tasks = $chatNio->Tasks();
$taskList = $tasks->getTaskList();
foreach ($taskList as $task) {
echo "\n" . $task->__toString();
}
$package = $chatNio->Pets()->Package();
echo "\ncert: " . ($package->isCert() ? "true" : "false");
echo "\nTeenager: " . ($package->isTeenager() ? "true" : "false");
$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();
�面两个可以删掉,只是方便导入秘钥
global $key;
tnio\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();