If online show this, else show this (not working)

I’m a bit stumped on this one. I thought that I had this all worked out, however it is only showing online channels instead of taking the ‘else’ statement that I have. I think I’m doing something wrong here, but I can’t figure it out.

<?php // Channels to pull data from $channels = array("everadiolive", "djyumene", "daopa"); // This part does stuff ... and things! No touchie! $streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true); foreach($streamData['streams'] as $chdata){ $name = $chdata['channel']['display_name']; $game = $chdata['channel']['game']; $url = $chdata['channel']['url']; if ($chdata != null) { echo "" .$name. " - Online - " .$game. "
"; } else { echo "" .$name. " - Offline
"; } } ?>

couple of errors,
The api for streams doesn’t return offline streams so any stream offline simply won’t appear in the streams array, which also means you can end up with an empty streams array if no ones streaming.

Secondly inside the foreach loop your calling to $chdata for the name, game and url, then after that checking if $chdata is null in the wrong order but that doesn’t matter as $chdata is never going to be null apart from some weird scenario where the api has returned a completely empty stream object inside the streams array. If there are no streams online at all from the api request you simply get an empty streams array, so the foreach loop will never iterate

Finally the if statement that checks if $chdata is null the else statement uses $name which is called from $chdata[‘channel’][‘display_name’] and if $chdata is null so $name is going to be too but as above because $chdata will never be null its never going to get called in that circumstance.

I made some edits, but now it’s doing the opposite of what I want.
See: http://icefaerie.org/twitch/

I know I’m still doing something wrong here. Anyways, this is the new edit:

<?php // Channels to pull the stuff from $live = array(); $channels = array("themittanidotcom", "djyumene", "daopa", "nightblue3", "cabochardlol", "moanygamer"); // This part does stuff ... and things! No touchie! $streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true); foreach ($channels as $name) $live[$name] = null; foreach ($streamData["streams"] as $chdata) $live[$stream["channel"]["login"]] = $stream["channel"]; $game = $chdata['channel']['game']; $url = $chdata['channel']['url']; foreach ($live as $name => $channel) { if($channel != null){ echo "$name $game Online
"; } else{ echo "$name Offline
"; } } ?>

What I’m seeing here:

You create an empty array $live, and an array $channels with the names you want to check.
Cool so far.

You then get all the objects from the online streams.
For every String in $channels, you set $live[$name] to null
So:
$live[“themittanidotcom”] = null;
$live[“djyumene”] = null; etc etc.

Then for every online Stream, you get all the “streams” array in the json.
You loop trough all the “streams” array elements in the json and call it $chdata

THEN you call $stream. what is $stream?
what is [“login”]? I don’t see that one anywhere in the json from the API

I suppose you mean, you get the name from that $chdata element: $chdata[“channel”][“name”]
so in $live, as key you use the streamer name, as value you put in the channel JSON

AFAIK apart from these mistakes I described above, the for each loop should work correctly.

one more note: $game and $url don’t work like you think, since you do not close/open the for each loop anywhere, the $game and $url will fail and be null, as the for loop will only work for the next 1 statement. (use brackets to include them)

even if you included them in the for each, they would return the game/url from the last streamer
So I’m not sure what the purpose of these is, but my best bet is, you would want to simply get these directly from the channel JSON object of that streamer. the way you try to extract it to a variable, either requires you to extract it in the very last for loop you posted. - or create an array with games/urls (which would be redundant)

EDIT:

   <?php
// Channels to pull the stuff from
$live = array();
$channels = array("themittanidotcom", "djyumene", "daopa", "nightblue3", "cabochardlol", "moanygamer");

// This part does stuff ... and things! No touchie!
$streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true);

foreach ($channels as $name) {     ////// tmake sure the keys exist for later
      $live[$name] = null;
}

foreach ($streamData["streams"] as $chdata)  {
     $live[$chdata["channel"]["name"]] = $stream["channel"];// For every live stream, use the name as key, channel JSON object as value
}
foreach ($live as $name => $channel) {
    if($channel != null){
           $game = $channel['game'];     // you can load these here
           $url = $channel['url'];
            echo "<a href='http://twitch.tv/$name/embed' target='tbox'><b>$name $game Online</b></a><br>";
     } else{
            echo "<a href='http://twitch.tv/$name/embed' target='tbox'><b>$name Offline</b></a><br>";
     }
 }
?>

note: not the most beautifull or correct, but (I think) it works, simply reformatted yours

1 Like

Thanks for the help there! That did help a lot! I only made one edit, there was still an instance of the $stream, which when I changed that over to $chandata now shows if it’s online or offline. Almost there, the issue now is that not getting the game for each specific channel that’s online. When it pulls that information, it’s showing the same game for all the online streams.

In this case, one is playing Mad Max, one if playing Eve Online, and one is playing League of Legends, but they all show up as playing Mad Max.

This can be seen here: http://icefaerie.org/twitch/

<?php // Channels to pull the stuff from $live = array(); $channels = array("themittanidotcom", "djyumene", "daopa", "nightblue3", "cabochardlol", "moanygamer"); // This part does stuff ... and things! No touchie! $streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true); foreach ($channels as $name) { $live[$name] = null; } foreach ($streamData["streams"] as $chdata) { $live[$chdata["channel"]["name"]] = $chdata["channel"]; } foreach ($live as $name => $channel) { if($channel != null){ $game = $chdata['channel']['game']; echo " $name - Playing: $game
"; } else{ echo " $name is Offline
"; } } ?>

It also looks like that aside from the game, it also pulls the same channel image and amount of viewers. Not where what else to edit to make this work.

This is the new version I got which I tried to get the logo’s as well as the viewers. It works aside from it only showing the information from the first live stream it sees and then copies that to the other streams that are live. Best way to explain that, and again, I have the code on my test page

<?php

// Channels to pull the stuff from
$live = array();
$channels = array(“themittanidotcom”, “djyumene”, “mym_alkapone”, “Rightrevgoldstein”, “daopa”, “streamerhouse”, “cabochardlol”, “moanygamer”, “bajheera”, “lelaaone”, “Old_Bearded_Gamer”);

// This part does stuff … and things! No touchie!
$streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true);

    foreach ($channels as $name) { 
        $live[$name] = null; 
    }

    foreach ($streamData["streams"] as $chdata)  {
        $live[$chdata["channel"]["name"]] = $chdata["channel"];        
    }
        foreach ($live as $name => $channel) {
                    if($channel != null){
                            $game =         $chdata['channel']['game'];
                            $logo =         $chdata['channel']['logo'];
                            $viewers =         $chdata['viewers'];
                    echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='online.png' height='7' width='7'> $game<br>Viewers: $viewers</font></p>";
                    } else{
                    echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='offline.png' height='7' width='7'> Offline<br><br></font></p>";
                        }
        }

?>

If you compare mine to yours, you will see the mistake.

In the last For each, you use
$game = $chdata[‘channel’][‘game’]
$logo = $chdata[‘channel’][‘logo’];

Can you perhaps tell me where $chdata is defined?
is it in that loop, or is it set before and not changed?

now to fix it:

  foreach ($live as $name => $channel)

Every pair, you read the $name and the JSON OBJECT $channel corresponding to that name.
Perhaps, use that data to get the correct game?

as seen from my snippet before:

foreach ($live as $name => $channel) {
   if($channel != null){
       $game = $channel['game']; 
.... ETC
}
1 Like

That worked great! Thanks for explaining that. My last issue, well, 2 now, is that the logo’s for the offline channels, is there a way to pull those? it doesn’t show a value for that, that I can see anyways. Also, the viewers don’t show up anymore when I changed it to:

$viewers = $channel[‘viewers’];

This is my first time really working with the twitch api like this and getting it to do what I want it to.

There’s a problem there. the “Viewers” are not stored in the channel data, but in the stream data.
Instead of saving the $chdata[‘channel’], it would be best to just save the $chdata instead:

    $live = array();
$channels = array("themittanidotcom", "djyumene", "mym_alkapone", "Rightrevgoldstein", "daopa", "streamerhouse", "cabochardlol", "moanygamer", "bajheera", "lelaaone", "Old_Bearded_Gamer");

// This part does stuff ... and things! No touchie!
$streamData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/streams?channel=".implode(",",$channels)), true);

foreach ($channels as $name) { 
    $live[$name] = null; 
}

foreach ($streamData["streams"] as $stream)  {
    $live[$chdata["channel"]["name"]] = $stream;        
}
    foreach ($live as $name => $stream) {
                if($stream != null){
                        $game =         $stream['channel']['game'];
                        $logo =         $stream['channel']['logo'];
                        $viewers =         $stream['viewers'];
                echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='online.png' height='7' width='7'> $game<br>Viewers: $viewers</font></p>";
                } else{
                echo "<p><img src='$logo' height='45' width='45' style='float:left;'><a href='http://twitch.tv/$name/embed' target='tbox'><font style='font-size: 1rem;line-height: 1.4;white-space: nowrap;font-weight: 700;color: #B9A3E3;text-decoration: none;font-family: 'Open Sans',sans-serif;'>$name</font></a><br><font style='font-size: 0.75rem;line-height: 1;white-space: nowrap;font-weight: 700;color: #3366FF;text-decoration: none;font-family: 'Open Sans',sans-serif;'>Playing: <img src='offline.png' height='7' width='7'> Offline<br><br></font></p>";
                    }
    }

In your old code, you got the viewers of the last user every time.
as you can see in the last forloop, you need the entire stream object, not just the channel object.

DO note:
I renamed every $chdata to $stream, to make the variable more clear

Edit: for every offline channel, if you would want toget their logo use:
https://api.twitch.tv/kraken/channels/test_channel

You will have to use kraken/channels for every offline channel to get their logo, there is no faster way to do this.

1 Like

Thanks so much for the help, and that did help me very much! :slight_smile: Looks good now, aside from the offline images, but I’ll be working on that next.

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