Code not working - See who's online - PHP JSON

Not sure why, but I grabbed this code from another thread which is now close and it doesn’t matter with which accounts (even live ones) they all appear as offline. I just copy/paste straight into PHP, please help.

Is there something else that needs to be done in order to work properly?

$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>";
                    }
    }

That code works for me:

Is file_get_contents returning anything? Many times hosting providers disable remote URLs for this function for security. Personally I’d recommend looking into using cURL for that purpose.

but the weird thing is that this code below works to check if someone is online… Is there a way to grab their name and game they are playing with the function below? All I want to output is something like “[name] is playing [game]”

function is_channel_live( $channel )
{
	
    $request = json_decode( @file_get_contents( 'https://api.twitch.tv/kraken/streams/' . $channel ) );
    return ( ! is_null( $request->stream ) ) ? TRUE : FALSE;
	
}

Don’t use file_get_contents for HTTP urls it is a HUGE SECURITY RISK.

A LOT of hosts will drop this attempt (often silently)

Please use cURL instead.

$ch = curl_init('https://api.twitch.tv/kraken/streams/' . $channel );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($ch);
curl_close($ch);

$request = json_decode($r);

That is quick and nasty but will get you going.

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