macbookandrew / wp-youtube-live

Displays the current YouTube live video from a specified channel
20 stars 12 forks source link

Live Chat #4

Open redyor opened 6 years ago

redyor commented 6 years ago

Hi Matthew,

Great Plugin!! I am using it for a website and it is working great!!

The issue is that I also need to embed the live chat when available. Currently, I am using the following code in my function.php which does the trick :

add_shortcode( 'youtube_live_chat', 'output_youtube_live_chat' );

function output_youtube_live_chat(){
 ob_start();
        try {
    $youtube_options = get_option( 'youtube_live_settings' );
    $channelid = $youtube_options['youtube_live_channel_id'];       
    $videoId = getLiveVideoID($channelid);
    $output = '<iframe allowfullscreen="" frameborder="0" height="200" src="https://www.youtube.com/live_chat?v='.$videoId.'&embed_domain='.$_SERVER['SERVER_NAME'].'" width="100%"></iframe>';
    // Output the Chat URL
    //$output = "The Chat URL is https://www.youtube.com/live_chat?v=".$videoId;
} catch(Exception $e) {
    // Echo the generated error
    $output = "ERROR: ".$e->getMessage(); // should return false 
}
return $output;
}

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

Now with the code above I can just use this shortcode [youtube_live_chat] to display the live chat it is not using the API but just a simple iframe embed, the only issue is the way I am getting $videoId using this made-up function getLiveVideoID instead of the of requesting it from your plugin.

Is there any way to integrate a live chat shortcode with this plugin, I think a lot of people will find it very useful (especially that there is no plugin currently available that has the functionality to add the like chat).

Also, I have a few other unrelated notes/questions:

What is the difference between using the Youtube API V3 vs a simple iFrame embed? Doesn't the API have limitations/quota on the number of requests sent? What happens if the quota is reached? Would it be a good idea to fall back to a simple embed video iframe (cause I don't think that the embed iframe has such limitation does it?)

Thank again for sharing this great plugin!

Red.

macbookandrew commented 6 years ago

Hmm…interesting idea. I’ll keep it in mind.

Answers to your other questions