PHP code example of nargor / tiktok-live-connector-php
1. Go to this page and download the library: Download nargor/tiktok-live-connector-php 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/ */
nargor / tiktok-live-connector-php example snippets
React\EventLoop\Loop;
use TikTokLive\Event\WebcastEvent;
use TikTokLive\TikTokLiveConnection;
$loop = Loop::get();
$tiktokUsername = 'officialgeilegisela';
// Create a new wrapper object and pass the username
$connection = new TikTokLiveConnection($tiktokUsername, $loop);
// Connect to the chat
$connection->connect()->then(
fn ($state) => printf("Connected to roomId %s\n", $state['roomId']),
fn (\Throwable $e) => fwrite(STDERR, "Failed to connect: {$e->getMessage()}\n"),
);
// Listen for chat messages (comments)
$connection->on(WebcastEvent::CHAT, function (array $data): void {
$user = $data['user']['uniqueId'] ?? '?';
printf("%s writes: %s\n", $user, $data['comment']);
});
// And gifts sent to the streamer
$connection->on(WebcastEvent::GIFT, function (array $data): void {
$user = $data['user']['uniqueId'] ?? '?';
printf("%s sends giftId=%d x%d\n", $user, $data['giftId'], $data['repeatCount']);
});
// Drive the ReactPHP event loop (blocks until disconnect)
$loop->run();
$connection->on(WebcastEvent::ERROR, function (\Throwable $e): void {
fwrite(STDERR, "Error: {$e->getMessage()}\n");
});
$connection->on(WebcastEvent::RAW_MESSAGE, function (string $type, string $bytes): void {
printf("type=%s payload=%d bytes\n", $type, strlen($bytes));
// base64-encode and report in an issue for new message-type support:
// base64_encode($bytes)
});