Search code examples
javascriptphptwitch

Twitch related: Twitch Video player


I am having a problem with twitch video player.

When I add the video player to the website and define the username without a PHP var it loads like it should but as soon as I add a var from PHP it doesn't load the stream from twitch.

This is how the code looks like with out the php Var (JS):

<script src= "http://player.twitch.tv/js/embed/v2.js"></script>
<div id="{PLAYER_DIV_ID}"></div>
<script type="text/javascript">
    var options = {
        width: '100%',
        height: 480,
        channel: "channel", 
        //video: "{VIDEO_ID}"       
    };
    var player = new Twitch.Player("{PLAYER_DIV_ID}", options);
    player.setVolume(0.3);
</script>

This is the code with the PHP var (the one that won't work.

<script src= "http://player.twitch.tv/js/embed/v2.js"></script>
<div id="{PLAYER_DIV_ID}"></div>
<script type="text/javascript">
    var options = {
        width: '100%',
        height: 480,
        channel: "<?php $channel ?>", 
        //video: "{VIDEO_ID}"       
    };
    var player = new Twitch.Player("{PLAYER_DIV_ID}", options);
    player.setVolume(0.3);
</script>

There might be a stupid questions. This is the first time I add "PHP" to a "JS" script like this, so I might do something wrong.


Solution

  • There is a problem with your php code -

    You should do

    <?php echo $channel?>
    

    OR just

    <?= $channel ?>