由短房间号获取原始长房间号

https://api.live.bilibili.com/room/v1/Room/room_init?id=528
//id后是短房间号

直播弹幕服务器api

https://api.live.bilibili.com/room/v1/Room/playUrl?cid=21738461&qn=10000&platform=web //2019/12/31新
https://api.live.bilibili.com/api/player?id=cid:5441    
// 5441 即原始房间号
9.15新
https://api.live.bilibili.com/room/v1/Danmu/getConf?room_id=21144080&platform=pc//room_id是原房间号

直播推流信息

https://api.live.bilibili.com/room/v1/Room/playUrl?cid=5441
// 5441 即原始房间号

解码响应的数据体:

function decodeMessage($socket) {
    while (socket_last_error($socket)) {
        while ($out = socket_read($socket, 16)) {
            $res = @unpack('N', $out);
            if ($res[1] != 16) {
                break;
            }
        }
        $message = @socket_read($socket, $res[1] - 16);
        $resp = json_decode($message, true);
        switch ($resp['cmd']) {
                    case 'DANMU_MSG':    // 弹幕消息
                        // info[1]    弹幕内容
                        // info[2][1] 发送者昵称
                        echo $resp['info'][2][1] . " : " . $resp['info'][1] . PHP_EOL;
                        break;
                    case 'SEND_GIFT':    // 直播间送礼物信息
                        $data = $resp['data'];
                        // uname    发送者的昵称
                        // giftName 赠送的礼物名称
                        // unum     一次赠送的数量
                        // price    礼物的价值
                        echo $data['uname'] . ' 赠送' .  $data['num'] . '份' . $data['giftName'] . PHP_EOL;
                        break;
                    case 'WELCOME':     // 直播间欢迎信息               
                        break;
                    default:         // 未知的消息类型              
        }
    }
    socket_close($socket);
}

心跳包

// 发送心跳包
function sendHeartBeatPkg($socket) {
    // 包类型从数据包的 7 修改为心跳包的 2
    $str = pack('NnnNN', 16, 16, 1, 2, 1);
    socket_write($socket, $str, strlen($str));
}

Last modification:February 15th, 2020 at 02:53 pm
If you think my article is useful to you, please feel free to appreciate