How to embed Youtube live chat with url permanent?
You should be able to use YouTube Live Streaming API to get the id and use the Live Stream data for any needs you have.
Indeed, one of the use cases is:
- Associate video streams and broadcasts.
On this page, you have a PHP example on how to "Retrieve a channel's video streams". On that code, $streamItem is a LiveStream, which contains the id of the live stream and you can leverage that.
On a related note, the API also allows you to work with LiveBroadcasts, which contains the reference snippet.liveChatId
to link it to LiveChatMessages. The latter would allow you to work with the messages in any format you want too. Perhaps, this would suit your needs better. The previous page with example codes, also has a good example on how to "Retrieve a channel's broadcasts".
I could copy the codes here, but I think the best working example is well documented on the Reference of the API :)
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;
}