如何使用网址永久嵌入Youtube实时聊天?

gri*_*one 11 javascript php embed youtube url

频道直播的嵌入网址为:

https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID
Run Code Online (Sandbox Code Playgroud)

并且它有效但如果我想在其附近嵌入一个YouTube实时聊天以获取当前流媒体,我用于嵌入的URL是:

https://www.youtube.com/live_chat?v=VIDEOID&embed_domain=DOMAINURL 
Run Code Online (Sandbox Code Playgroud)

问题是:对于每个新的直播流,视频ID都会发生变化.因此嵌入式代码不再有效,并且下次流式传输不会显示聊天.我希望永久性的URL实时聊天对我的所有YouTube流媒体都有效,而不是每次都手动更改视频ID.怎么解决?也许使用PHP或javascript中的脚本来读取当前的YouTube URL并替换聊天嵌入URL中的视频ID?谢谢

Roe*_*lVB 7

您可以使用PHP获取视频ID,如下所示:

<?php

try {
    $videoId = getLiveVideoID('CHANNEL_ID');

    // Output the Chat URL
    echo "The Chat URL is https://www.youtube.com/live_chat?v=".$videoId;
} catch(Exception $e) {
    // Echo the generated error
    echo "ERROR: ".$e->getMessage();
}

// The method which finds the video ID
function getLiveVideoID($channelId)
{
    $videoId = null;

    // Fetch the livestream page
    if($data = file_get_contents('https://www.youtube.com/embed/live_stream?channel='.$channelId))
    {
        // Find the video ID in there
        if(preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches))
            $videoId = $matches[1];
        else
            throw new Exception('Couldn\'t find video ID');
    }
    else
        throw new Exception('Couldn\'t fetch data');

    return $videoId;
}
Run Code Online (Sandbox Code Playgroud)