Is streamer online, JSON help: /users/zigenzag/follows/

Im not sure how to work out using this method of getting certain streams online/offline status using this (https://api.twitch.tv/kraken/users/zigenzag/follows/channels) JSON file.

I attempted to use delay to detect the stream as i figure if the delay is 0 they are offline. This being because i have found there is always a delay between the host and the end user… in my experience anyway.

Im not the best coder in the galaxy so im sure ill be missing something but i had a check through the git and couldnt find a solution. Also if there is another method of getting multiple streams details in as few JSON calls as possible (to lower loading times of site) I would love to know.

Pastebin of example channel response (1/15) http://pastebin.com/H4v8PPRX

Thanks for looking into my problem guys,
Zigenzag

EDIT: Maybe its possible to add something to the JSON API?

The users/channels API does not tell you if they are live or not. For that you should use the Streams API. To get all live streams a user is following you can use /kraken/streams/followed, or if you just want multiple channels in the response you can use /kraken/streams?channel=riotgames,beyondthesummit.

1 Like

Thank you very much for your help Fugiman!

I will include the code bits I used to get the data from the API in case others have this issue…

$channels = array('Stream1','Stream2','Stream3') ;
$callAPI = implode(",",$channels);
$dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);

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

For anyone wondering the backslashes ( \ ) before the quotation marks ( " ) makes php ignore the quotes programmed meaning and just include them in the “echo” command.

I modified this a bit and can’t get it to close right… what am I missing?

<?php

$channels = array('Stream1','Stream2','Stream3') ;

$callAPI = implode(",",$channels);
$online = 'online.png';     
$offline = 'offline.png';   
$dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);

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

   echo "<img src='$online' alt='Online' />";
} else {
    echo "<img src='$offline' alt='Offline' />";
}

?>

there seems to be an extra { out there after that line?

the { after foreach is required, however you seem to be missing the closing }. I’m unsure what you mean by “can’t get it to close right”, can you clarify what problem you’re having?

Hi, the code is correct but this api with multi channel doesn’t allowed to display offline. If i used https://api.twitch.tv/kraken/streams/"$value" for multi channel’s online/offline, the page takes a long time to load. Any idea?

Hey there,

I used the code from here to let the video embed based on the three names on top. However, when i enter this code into a php file and i let it include into my htmlpage it just doesn’t pop up and blocks anything that has to go under the phpinclude.

<?php $channels = array('towelliee','celplays','zoeksy') ; $callAPI = implode(",",$channels); $dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true); foreach($dataArray['streams'] as $mydata){ if($mydata['_id'] != null){ $name = $mydata['channel']['display_name'];
            echo "<h4>".$name"</h4><iframe src='http://www.twitch.tv/" .$name "/embed' frameborder='0' scrolling='no' height='210' width='400' style=' -webkit-box-shadow: 0px 0px 5px 5px rgba(0, 0, 0, 0.75); -moz-box-shadow:0px 0px 5px 5px rgba(0, 0, 0, 0.75); box-shadow:0px 0px 5px 5px rgba(0, 0, 0, 0.75)'></iframe>";
                }else{
            echo "<p>Need to fill later</p>";
        }
  }

?>

                        <?php include("online.php"); ?>

The php file is indeed online.php but just doesn’t work.
Anyone ideas why?

Does it give any error? One possibility is that your host has blocked file_get_contents from accessing remote URLs, in which case you’d have to use CURL to fetch the data.

<?php

$channels = array(‘monstercat’, ‘DeadlyFamous’, ‘celplays’) ;
$callAPI = implode(“,”,$channels);
$arrContextOptions=array(
“ssl”=>array(
“verify_peer”=>false,
“verify_peer_name”=>false,
),
);
$dataArray = json_decode(file_get_contents(‘https://api.twitch.tv/kraken/streams?channel=’ . $callAPI, false, stream_context_create($arrContextOptions)), true);

foreach($dataArray[‘streams’] as $mydata){

if($mydata['_id'] != null){
    $name      = $mydata['channel']['display_name'];
    $game      = $mydata['channel']['game'];
    $url       = $mydata['channel']['url'];       
    echo "<h2>" .$name. " - " .$game. "</h2>
            <iframe src='http://www.twitch.tv/" .$name. "/embed' frameborder='0' scrolling='no' height='450' width='620' style='display: table; margin:0 auto; -webkit-box-shadow: 0px 0px 5px 5px rgba(0, 0, 0, 0.75); -moz-box-shadow:0px 0px 5px 5px rgba(0, 0, 0, 0.75); box-shadow:0px 0px 5px 5px rgba(0, 0, 0, 0.75);'>
            </iframe>";
}else{
    echo "<p>test</p>";
}

}
?>

seemed to do it, in case others have the same problem i’ll keep this code here. Only thing in this code that doesn’t work is that if every of the streamers is offline the

echo "<p>test</p>";

does not show up.

After the foreach closes add:

if($dataArray['streams'] == null or $dataArray['streams'] == "")
  {
  echo	"<p>There are currently no live streams.</p>";
  echo	"<p>Refresh this page to retrieve live streams or check back later!</p>";
  }

This will output the above if $dataArray[‘streams’] returns null or “”.