Help reviving an old script?

Just need a little bit help getting this to work… I’m a huge noob :frowning:


<html>
<head>
<title>Your Title Goes Here</title>
</head>
<body>
<?php
/* Mucked together by Matt_Thomas @ Twitch.tv
   Was created in response to: https://discuss.dev.twitch.com/t/is-there-a-way-to-embed-twitch-on-your-site-so-that-only-the-most-viewed-streamer-plays/3309
   This will take the $channels array list of users, find the streamer in your list ($channels) with the most viewers, then
   display it's stream.  If none of the streamers are live, it will find the current twitch streamer with the most viewers instead.
   This can be modified easily to include any list of streamers.  You can also change the fall back to be a specific game. Enjoy!
   Shoutouts to Kevo and https://discuss.dev.twitch.com/t/is-streamer-online-json-help-users-zigenzag-follows/946 - Where I started from.
   matt AT mrthomas DOT org */

/* Old unsupported API for team members: http://api.twitch.tv/api/team/teamkitty/all_channels.json - Might use this until kraken has team member listing, or until it changes */

$channels = array("fhaelin", "kittyplaysgames", "schyax", "cutenaomi", "cincinbear", "brialeigh", "juliehaze", "pallyberry"); /* For now, we use a manually set list of streamers */
$callAPI = implode(",",$channels); /* Clean our list for next step */
$dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?client_id=XXXXXXXXXXXX&channel=' . $callAPI), true); /* Pull the data from api.twitch.tv/kraken for each streamer in our list */

/* The query could just add '&limit=1' since the API will return the streamers in order of highest viewers to lowest. This speeds things up.*/
/* $dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI . '&limit=1'), true); */

$max_viewers=0; /* Set a value to start stepping from */
$name='*** ERROR!!1! ***'; /* Set a value to name incase something goes wrong */

if ($dataArray['streams'] != null) { /* Make sure we get a response from the API */
    foreach($dataArray['streams'] as $mydata){ /* Build an array with online streamers from our list */
        if($mydata['_id'] != null){  /* Make sure the streamer has data in the API */
            $viewers = $mydata['viewers'];  /* Store value of viewers from API as $viewers */
            if ( $viewers > $max_viewers ) {  /* Should always be more than 0 at this point */
                $max_viewers = $viewers;   /* Set new nonzero value to be $max_viewers and loop 'foreach' until we have the highest viewer count */
                $name = $mydata['channel']['display_name'];  /* Store the display_name from API for user with highest viewer count */
            }
        }
    }
}
else {
    /* None of the $channels I wanted are avaiable now, so fetch the most popular one */
    $backupArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?client_id=XXXXXXXXXX&limit=1' . $callAPI), true); 
    if ( $backupArray != null ) {
        foreach($backupArray['streams'] as $mydata){
            if($mydata['_id'] != null) {
                $name = $mydata['channel']['display_name'];
                $viewers = $mydata['viewers'];
            }
            else {
                echo "Error in results from api.twitch.tv, cannot fetch a channel.";  /* API responded but not with expected data */
            }
        }
    }
    else {
        echo "Error in results from api.twitch.tv, probably cannot connect to server."; /* API probably did not respond at all */
    }
}
?>
<center>
<h1> Streamer: <?php echo "$name" ?> </h1>
<iframe 
        src="http://player.twitch.tv/?channel='.$channel.'" 
        height="720" 
        width="1280" 
        frameborder="0" 
        scrolling="no"
        allowfullscreen="true">
    </iframe>
</center>
</body>
</html>

I added the iframe line as well as the client_id=XXXXXX in the kraken link. Originally it wouldnt pull the channel name but I got it to do that… now nothing happens when I click play on the iframe player :frowning: I’m thinking that the player is not associating itself with the code? I know absolutely nothing about code so any help would be appreciated!

EDIT: Got the code pasted

Try posting the code inside a pair of triple grave accents (`). Ex:

Alright! Thanks so much for that george :slight_smile:

<iframe 
        src="http://player.twitch.tv/?channel='.$channel.'" 
        height="720" ...

$channel is undefined. I think you’re looking for $name.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.