Im trying to fetch data from twitch and have it output it using different css whether the streamer is online or offline. But i cant get it to work, it just says the streamer is offline.
function twitch_streams_widget() {
// Try to retrieve a stream from cache to avoid external calls
$stream = wp_cache_get( 'featured_stream' , 'apoc' );
if ( false === $stream ) {
// The list of valid streamers
$streamers = array(
'guratw' => 'guratw'
);
// Shuffle the array
$keys = array_keys( $streamers );
shuffle( $keys );
$new = array();
foreach($keys as $key) {
$new[$key] = $streamers[$key];
}
$streamers = $new;
// Loop through streamers in a random order, and pull their info
foreach ( $streamers as $name => $user ) {
// Get the Twitch data
$twitch_response = json_decode( @file_get_contents( "https://api.twitch.tv/kraken/streams/" . $user ) );
$is_online = isset( $twitch_response->stream->game );
$game = $is_online ? $twitch_response->stream->game : false;
// Are they online and playing Titanfall?
if ( $is_online && $game == "Titanfall" ) break;
}
// If we have an online user, report it
$url = 'http://twitch.tv/'.$user;
$viewers = $is_online ? intval( $twitch_response->stream->viewers ) . " Viewers" : "Offline";
$icon = $is_online ? '<i class="icon-ok"></i>' : '<i class="icon-remove"></i>';
$status = $is_online ? 'online' : 'offline';
$stream = '<a class="twitch-stream-name '.$status.'" href="'.$url.'" target="_blank">'.$name.' '.$icon.'</a><span class="activity-count twitch-stream-count">'.$viewers.'</span>';
// Set it to the cache
wp_cache_set( 'featured_stream' , $stream , 'apoc' , 180 );
}
// Display the widget ?>
<div class="widget featured-stream-widget">
<header class="widget-header"><h3 class="widget-title">Featured Stream</h3></header>
<div id="featured-stream">
<?php echo $stream; ?>
</div>
</div>
<?php
}
Anyone that can tell me what the problem is ?