I have this snippet below, I'm trying to query if a cast is online or not (That part works when I take it outside the while loop) but then in the while loop run through until I find an online cast and then keep it there.

If I remove the break; in the ($stream == null) it will run the array but only for my first $url (So, the embed will run 4 times but only query the first $url).

So, if the stream is online. It will show all the streams in the array despite them being offline/online.

How can I make it so that it queries the array and executes the first embed of an online stream?

<?php
    //On/Off
    $url = "";
    $json = file_get_contents($url);
    $json = json_decode($json);
    $stream =  $json->stream;

        $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane"); 
        while (list(, $val) = each($streamers)) {

                $url = "".$val;
            //var_dump($json);
            $url1 = "". $val;
            $json1 = file_get_contents($url1);
            $json1 = json_decode($json1);
            $logo =  $json->preview->medium;
            $game = $json1->channels->game;

            if($stream != null){                    
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';
               echo'<p class="intro-text"></p>';
               echo "Live";
               echo '<iframe src="http://www.twitch.tv/'. $val .'/embed" frameborder="0" scrolling="no" height="378" width="620"></iframe>';  

            }
            if($stream == null){
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';

               echo'<p class="intro-text">We are all offline at the moment.</p>';
               echo'<p class="intro-text">When we are online, this page will change</p>';
               break;
        }
        echo $val;
    }
?>

Dani AI

Generated

Nice, simple fix from — using a flag so the loop only produces one embed is the right idea for this specific goal. The original symptom came from exiting the loop at the wrong point and/or mixing the “check status” and “render” steps, which makes the flow brittle. A cleaner pattern is: 1) determine which channel(s) are live, 2) choose the first live channel according to your array order, then 3) render the page (embed the chosen channel while still outputting status for the rest).

Also modernize the loop and JSON handling. The old while(list(...)=each(...)) pattern is deprecated and removed in newer PHP — use foreach for clarity and future compatibility. Inspect the API JSON with a quick dump so the code reads the correct properties (preview is often nested under the stream object rather than at the root). (php.net)

If you want to avoid N HTTP calls (one per streamer) and make the logic both faster and more reliable, ask the Twitch API for multiple channels in one request and then pick the first online one in your original order. The Helix "Get Streams" endpoint accepts multiple user_login parameters (up to 100) and returns only the live streams; build a set from that response and iterate your original streamer list to find the first match to embed. Also note the older Kraken endpoints are deprecated — migrate to Helix. (dev.twitch.tv)

Finally, make network and error handling robust: use curl or an HTTP stream context with explicit connect/read timeouts, check HTTP status codes and JSON decode results, and implement short caching (30-60 seconds) to avoid hitting rate limits. Watch the API rate-limit headers and fail gracefully (show “all offline” fallback) if the API is unreachable. These small changes will make the boolean fix from durable and keep ’s page responsive and future-proof. (php.net)

Recommended Answers

All 4 Replies

How can I make it so that it queries the array and executes the first embed of an online stream?

Add a break; to exit your while loop.

Hey Prit,

I've added the break into the while loop instead of the if statements. It runs through array hits the first and then defines if it's online or offline.

    $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane");

If I make the first array "DeathridgeCraft" it will go live. So he is live at the moment but because it's after an offline channel it won't relay through the array.

Here is my full script:

<?php
        $streamers = array("greatbritishbg", "Gecko194", "jayw89","DethridgeCraft","reninsane"); 
        while (list(, $val) = each($streamers)) {

            $url = "".$val;
            $json = file_get_contents($url);
            $json = json_decode($json);
            $stream =  $json->stream;


            //var_dump($json);
            $url1 = "". $val;
            $json1 = file_get_contents($url1);
            $json1 = json_decode($json1);
            $logo =  $json->preview->medium;
            $game = $json1->channels->game;

            if($stream != null){                    
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';
               echo'<p class="intro-text"></p>';
               echo "Live";
               echo '<iframe src="http://www.twitch.tv/'. $val .'/embed" frameborder="0" scrolling="no" height="378" width="620"></iframe>';

            }
            if($stream == null){
               echo'<header class="intro">';
               echo'<div class="intro-body">';
               echo'<div class="container">';
               echo'<div class="row">';
               echo'<div class="col-md-8 col-md-offset-2">';
               echo'<h1 class="brand-heading">GBBG</h1>';

               echo'<p class="intro-text">We are all offline at the moment.</p>';
               echo'<p class="intro-text">When we are online, this page will change</p>';

        }
        break;
        echo $val;
    }
?>

Oh wait, I misunderstood. You want to show all streams but embed only one online one.

Remove the break. Add a boolean before the while $embedded = false; Around line 28 put:

if (!$embedded)
{
    $embedded = true;
    // Line 28 here
}

Thanks very much prit :) Rather simple solution, I can't believe I didn't think of it ^>^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.