Can't get "offline" names to show up

I am using the following:

$channels = array('streamername') ;
$callAPI = implode(",",$channels);
$dataArray = json_decode(@file_get_contents('' . $callAPI), true);
$online = 'online.png';
$offline = 'offline.png';

foreach($dataArray['streams'] as $mydata){
if($mydata['_id'] != null){
    $name         = $mydata['channel']['display_name'];
    $game        = $mydata['channel']['game'];
    $url        = $mydata['channel']['url'];        

    echo "<a href=\"$url\">" . $name . " - " . $game . "</a> <img src='$online'> <br> ";
        echo "<a href=\"$url\">" . $name . " </a> <img src='$offline'> <br> ";

Is there something I am missing that its not showing the “offline” streamers?

with streamers online, it shows all the correct info, but if the streamer is “offline” its not showing anything at all.

the response from /kraken/streams won’t contain offline streamers, so checking if stream._id == null is not the proper way to detect if a stream is live or not. The only way to do that is to build your own data structure that contains all the streamers you care about and whether they are in the response from /kraken/streams or not.

So, you are saying you can’t really use an “else” clause, unless you setup your own coding based off of twitch’s API ?

Right, there is no API endpoint that says “here’s all the channels you asked for and whether they are live or not”. There’s only “here’s the live channels out of what you asked for” or “here’s the users you asked for”. So to build the data of “who is live or not” you need to merge the input data and output data.

Example code:

// Fetch all the data we need
$live = array();
$channels = array("streamername");
$data = json_decode(@file_get_contents("".implode(",",$channels)), true);

// Copy the channels we asked for into the composite associative array
foreach ($channels as $name) $live[$name] = null;

// Then copy the stream data into the composite associative array
foreach ($data["streams"] as $stream) $live[$stream["channel"]["login"]] = $stream["channel"];

foreach ($live as $name => $channel) {
    if ($channel != null) {
        echo "<a href=\"$channel['url']\">$channel["display_name"] - $channel["game"]</a> <img src=\"online.png\"> <br>";
    } else {
        echo "$name <img src=\"offline.png\"> <br>";

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