How to Build a Real‑Time Bilibili Danmu Integration with PHP‑Webman
This guide explains how to set up the PHP‑Bilibili‑Danmu framework built on Webman, configure Protocol Buffers, run database migrations, connect to Bilibili live rooms, handle danmu and interaction events, and verify the installation using a management dashboard.
Overview
PHP‑Bilibili‑Danmu is an advanced integration framework built on the high‑performance Webman PHP framework. It connects to Bilibili’s live‑stream platform, captures, parses and stores real‑time chat messages (danmu), gifts, user interactions and other live events.
The system relies on Google’s Protocol Buffers for efficient, structured message handling, enabling reliable processing of large streams of data.
Main Features
Danmu handling : capture, parse and store live chat messages.
Gift tracking : record and process gift transactions.
Live integration : connect to multiple rooms simultaneously.
User management : track interactions and maintain user data.
Management dashboard : comprehensive monitoring and configuration UI.
Role‑based access control : configurable permission management.
Internationalization : multi‑language support via a translation system.
Quick Start
Step 1 – Install
Clone the repository and install dependencies with Composer.
git clone https://github.com/zxc7563598/php-bilibili-danmu.git
cd php-bilibili-danmu
composer installCreate a .env file with minimal configuration (database, Redis, etc.).
APP_DEBUG=true
APP_DEFAULT_TIMEZONE=Asia/Shanghai
# Database
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=bilibili_danmu
DB_USERNAME=root
DB_PASSWORD=your_password
# Redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379Save your Bilibili cookies to runtime/tmp/cookie.cfg (the bili_ticket field will be refreshed automatically).
Step 2 – Database migration
Run the migration to create required tables. vendor/bin/phinx migrate This creates bl_danmu_logs for danmu messages and bl_lives for live‑stream tracking.
Step 3 – Connect to a live stream
Create a script (e.g., connect.php) that instantiates Bililive\Client with a room ID and cookie, then start the client.
<?php
require __DIR__ . '/vendor/autoload.php';
use app\core\RobotServices;
use Hejunjie\Bililive;
$roomId = $argv[1] ?? 123456;
$cookie = RobotServices::getCookie();
$client = new Bililive\Client([
'room_id' => $roomId,
'cookie' => $cookie,
]);
echo "Connecting to room $roomId...
";
$client->start();Step 4 – Process danmu messages
Extend the script with event handlers for DANMU_MSG and INTERACT_WORD_V2 to store messages in the database and output user interactions.
<?php
// ... (initialisation code)
$client->on('DANMU_MSG', function($data) {
$danmu = new DanmuLogs();
$danmu->uid = $data['info'][2][0];
$danmu->uname = $data['info'][2][1];
$danmu->content = $data['info'][1];
$danmu->roomid = $data['roomid'];
$danmu->save();
echo "Danmu from {$data['info'][2][1]}: {$data['info'][1]}
";
});
$client->on('INTERACT_WORD_V2', function($data) {
$stream = new Google\Protobuf\Internal\CodedInputStream($data['data']);
$msg = new InteractWordV2();
$msg->mergeFromStream($stream);
echo "User interaction: {$msg->getUname()} (type: {$msg->getMsgType()})
";
});
$client->start();Step 5 – Run the application
Start the Webman HTTP server. php start.php start For production, run as a daemon:
php start.php start -dVerification
Open the admin dashboard (e.g., http://localhost:8787/admin).
Check the logs to confirm a successful Bilibili API connection.
Verify that database migrations completed without errors.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
