如何使用url永久嵌入Youtube在线聊天? [英] How to embed Youtube live chat with url permanent?

查看:122
本文介绍了如何使用url永久嵌入Youtube在线聊天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

频道直播流的嵌入URL为:

The embed URL for a channel's live stream is:

https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID

它可以工作,但是如果我想在附近嵌入YouTube实时聊天,当前用于嵌入的URL的流式传输是:

and it works but if I want to embed near at it a YouTube live chat for current streaming the URL that I use for the embed is:

https://www.youtube.com/live_chat?v=VIDEOID&embed_domain=DOMAINURL 

问题是这样的:对于每个新的实时流,视频ID都会更改。这样一来,嵌入的代码将不再有效,也不会在下一次流式传输中显示聊天。我希望永久性的URL实时聊天对我所有的YouTube流
有效,而无需每次手动更改视频ID。
如何解决?也许使用PHP或javascript中的脚本读取当前YouTube URL并替换聊天嵌入URL中的视频ID?
谢谢

The problem is this: for every new live stream the Video ID changes. So that the embedded code isn't valid anymore and chat isn't displayed for next streaming.I want a permanent URL live chat valid for all my YouTube streaming without change video id manually every time. How to resolve? Perhaps with a script in PHP or javascript that read current YouTube URL and replace video id in chat embed URL? thanks

推荐答案

您可以使用PHP这样获得视频ID:

You could get the video ID using PHP like this:

<?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;
}

这篇关于如何使用url永久嵌入Youtube在线聊天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆