0

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 = "https://api.twitch.tv/kraken/streams/greatbritishbg";
    $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 = "https://api.twitch.tv/kraken/streams/".$val;
            //var_dump($json);
            $url1 = "https://api.twitch.tv/kraken/channels/". $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;
    }
?>

Edited by pritaeas: Moved to PHP.

2
Contributors
4
Replies
14
Views
2 Years
Discussion Span
Last Post by bradly.spicer
1

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.

0

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 = "https://api.twitch.tv/kraken/streams/".$val;
            $json = file_get_contents($url);
            $json = json_decode($json);
            $stream =  $json->stream;


            //var_dump($json);
            $url1 = "https://api.twitch.tv/kraken/channels/". $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;
    }
?>
1

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
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.