Is there a way to embed twitch on your site so that only the most viewed streamer plays?

I’m trying to figure out how to embed a stream to only play the stream that has the most viewers at the current time. If that makes sense, is this possible?

Hi. I’m horrible at PHP, I’m a sysadmin, I don’t really do programming really. I’d call what I do scripting, but this sounded like a fun little adventure for me.

I’ve built a working example of what you want, all you have to do is edit the array

$channels = array(“aimzatchu”, “kittyplaysgames”, “schyax”, “cutenaomi”, “cincinbear”, “brialeigh”, “juliehaze”, “pallyberry”);

To be your list of streamers.
It’s currently set to use a handful of streamers from (TeamKitty)

You can download the test.php right from my site or look at the example in action right here


Added test.php.txt for easy viewing.

Thanks Matt works perfectly! Glad to see there are still nice people on the internet!

In short, you’ll want to hit and use the first object in streams to populate the embed player.

I updated my example after talking to someone who does programming and knew PHP.

Increased the speed of finding the streamer on the list with the most viewers by stepping through it once.

Added a fallback, that if none of the original listed streamers are found, find the top twitch streamer and use them instead. (From [quote=“Fugiman, post:4, topic:3309”]

Added some basic error messages that will give a hint to what went wrong, if anything at all.

I also put in some comments for helping people figure out what’s going on.

The whole example is visible at my example page
-See it in action-
-See the code-

Good job! Glad to see you got it working. Just a couple of notes:

  1. The API response will already be ordered by number of viewers, so you can just select the first element in the array
  2. You check for stream == null, but I believe it can also just be an empty array? Maybe this is type coercion saving you here. I wouldn’t recommend relying on it.
  3. There’s no need to iterate over the fallback API response either. Just select the first element.

The first step uses a static array set in $channels, and each call to the API will only give 1 response at a time. (as I understand) So for the first step I have to figure out who in the list is live, with the most viewers. (I thought)

Could I ask API for all of them at once with multiple ?channel= requests?

The second step uses what you set and only takes the first streamer on /streams/

I gave in and RTFM. I learned, comma delimited on ?channel=

I was working from a previous setup and didn’t realize what my $callAPI was doing. (I didn’t know anything about PHP before starting this) lol.

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