如何永久嵌入 Youtube 实时聊天 url?
How to embed Youtube live chat with url permanent?
频道直播的嵌入 URL 是:
https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID
并且有效,但如果我想在其附近嵌入 YouTube 实时聊天以进行当前流式传输,我用于嵌入的 URL 是:
https://www.youtube.com/live_chat?v=VIDEOID&embed_domain=DOMAINURL
问题是这样的:对于每个新的直播流,视频 ID 都会发生变化。因此嵌入的代码不再有效并且聊天不会显示下一个 streaming.I 想要一个永久的 URL 实时聊天对我所有的 YouTube 流媒体有效
无需每次手动更改视频 ID。
如何解决?也许使用 PHP 或 javascript 中的脚本读取当前 YouTube URL 并替换聊天嵌入 URL 中的视频 ID?
谢谢
您可以像这样使用 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;
}
您应该能够使用 YouTube Live Streaming API 获取 ID 并使用实时流数据来满足您的任何需求。
确实,其中一个用例是:
- 关联视频流和广播。
在 this page, you have a PHP example on how to "Retrieve a channel's video streams". On that code, $streamItem is a LiveStream 上,其中包含直播流的 ID,您可以利用它。
在相关说明中,API 还允许您使用 LiveBroadcasts, which contains the reference snippet.liveChatId
to link it to LiveChatMessages。后者将允许您以任何您想要的格式处理消息。也许,这会更好地满足您的需求。带有示例代码的前一页也有一个关于如何 "Retrieve a channel's broadcasts".
的很好的示例
我可以在这里复制代码,但我认为最好的工作示例在 API 的参考资料中有详细记录:)
频道直播的嵌入 URL 是:
https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID
并且有效,但如果我想在其附近嵌入 YouTube 实时聊天以进行当前流式传输,我用于嵌入的 URL 是:
https://www.youtube.com/live_chat?v=VIDEOID&embed_domain=DOMAINURL
问题是这样的:对于每个新的直播流,视频 ID 都会发生变化。因此嵌入的代码不再有效并且聊天不会显示下一个 streaming.I 想要一个永久的 URL 实时聊天对我所有的 YouTube 流媒体有效 无需每次手动更改视频 ID。 如何解决?也许使用 PHP 或 javascript 中的脚本读取当前 YouTube URL 并替换聊天嵌入 URL 中的视频 ID? 谢谢
您可以像这样使用 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;
}
您应该能够使用 YouTube Live Streaming API 获取 ID 并使用实时流数据来满足您的任何需求。
确实,其中一个用例是:
- 关联视频流和广播。
在 this page, you have a PHP example on how to "Retrieve a channel's video streams". On that code, $streamItem is a LiveStream 上,其中包含直播流的 ID,您可以利用它。
在相关说明中,API 还允许您使用 LiveBroadcasts, which contains the reference snippet.liveChatId
to link it to LiveChatMessages。后者将允许您以任何您想要的格式处理消息。也许,这会更好地满足您的需求。带有示例代码的前一页也有一个关于如何 "Retrieve a channel's broadcasts".
我可以在这里复制代码,但我认为最好的工作示例在 API 的参考资料中有详细记录:)