Show which of my friends is on and with how many viewers embedded on my website

I’m sorry new to PHP. I run a website with my friends. I have given them all their own page. Is there a way to show who is on and how many viewers listed? I found this code by Matt_Thomas. I have tried EVERYTHING and cant get it to work. Can someone please help??

Thank you

<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("aimzatchu", "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?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?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>
<object type="application/x-shockwave-flash" height="710" width="1200" id="live_embed_player_flash" data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=<?php echo "$name" ?>"
bgcolor="#000000">
<param name="allowFullScreen" value="true"/>
<param name="allowScriptAccess" value="always"/>
<param name="allowNetworking" value="all"/>
<param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf"/>
<param name="flashvars" value="hostname=www.twitch.tv&channel=<?php echo $name ?>&auto_play=true&start_volume=25"/></object>
</center>
</body>
</html>

Can you maybe specify about what kind of friends you are talking about? Do you want the live channels of your reallife friends to be in a list, which you then use to check every single one of them if they are streaming right now, or do you want to get the people you have added as a friend on Twitch to get checked? Also please format your code, because i cannot tell anything from what you sent. You can simply do that, by uploading it to something like hastebin.com or embedding it in here with the Preformatted Text button in the texteditor in here.

The problem here is that you are using file_get_contents and file_get_contents doesn’t work on most hosts for security reasons.

Code needs to be updated/revised to use cURL instead of file_get_contents

But you’d have to describe the actual error you are getting.

Yes my real friends. So I have a bunch of friends that are on. I want the website to list all my friends and I can then pick which one I want to see.

Oh okay. So as @BarryCarlyon said you should do that with cURL([see here for the official curl PHP manual])(https://www.php.net/manual/en/book.curl.php). Although something like that is already implemented in Twitch. It would require one of your friends to be a Twitch Partner, which is probably not the situation here. But if one of your friends is a partner, he can just create a Twitch team and invite the other ones in there. Then you have an overview page about the team, where everyone is listed(who is currently streaming) and is sorted by viewer count. Read here for more: https://help.twitch.tv/s/article/twitch-teams?language=en_US

But I guess, you want to go with the programming it yourself approach. So take a look at the manual page of cURL and just put a reply here if you got any questions or encounter any issues ^^

I get “error in results from api.twitch.tv, probably cannot connect to server.

Yeah, it’s a “DJ” page. All my friends are DJ’s from all over the United States and I don’t know when they go live. So having this on my page would just help me.

Ok I will check out the Curl portion and I will get back to you.

Thank you so much.

The script also needs to be updated to use https://dev.twitch.tv/docs/embed/ instead of the widget it currently tries to use

Ok, yikes I’m new to all this so I will try. If not then thank you for your time.

1 Like

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