Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How to embed Youtube live chat with dynamically updated url to the livestream?

$
0
0

Hey guys thanks for help!

I would like to permanently embed a YouTube live stream with chat to my website.

The problem is, that YouTube doesn't give permanent urls. Every live stream has unique stream ID.

However you can embed a YouTube livestream via link that always connects to the currently scheduled upcoming live stream VIDEOID:

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

This solves first half of the problem. The second half is the Live Chat which you can only embed if you know the live stream VIDEOID.

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

So what I would like to achieve:

  1. Find the VIDEOID of the current upcoming Live Stream from this URL: https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID

  2. Get a Live Chat url containing that VIDEOID so I can embed it on my website and which will update every time the new live stream with new VIDEOID will be published.

I found the PHP code here that is supposed to achieve it but it returns an error "Couldn't find video ID". Probably because this code is from 2017 and the regex can't properly locate the VIDEOID in YouTube's updated code but I have no coding skills so I can't fix it.

Here is the code that returns an error:

<?phptry {    $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 IDfunction 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;}

Can someone help me fix this code so it works? Thanks


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>