Skip to content

Instantly share code, notes, and snippets.

@jbouse
Created August 26, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbouse/a8a80e56c0a59e726ff78ddaa5231a3c to your computer and use it in GitHub Desktop.
Save jbouse/a8a80e56c0a59e726ff78ddaa5231a3c to your computer and use it in GitHub Desktop.
<?php
try {
if (count(["cid"]) == 0) {
$channelId = "UCL7oxb4N7uxEOHPlhFFxAhg";
}else{
$channelId = ["cid"];
}
$videoId = getLiveVideoID($channelId);
$chatUrl = "https://www.youtube.com/live_chat?v=" . trim($videoId) . "?is_popout=1"
?>
<script type="text/javascript">
window.location = "<?php echo $chatUrl; ?>"
</script>
<br>
The live chat URL is <?php echo $chatUrl; ?>
<?php
} 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('Could not find video ID');
}
else
throw new Exception('Could not fetch data');
return $videoId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment