Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Chat #4

Open
redyor opened this issue Apr 25, 2018 · 1 comment
Open

Live Chat #4

redyor opened this issue Apr 25, 2018 · 1 comment

Comments

@redyor
Copy link

redyor commented Apr 25, 2018

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
Copy link
Owner

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

Answers to your other questions

  • YouTube API v3 vs. iframe: this plugin does use iframe embeds, but it uses the data API to find out if there is a live video
  • API limits: please read this section of the readme and it should answer most of your questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants