Simple Online/Offline Script Help Needed

Hey guys,

I’ve been trying to get a very simple PHP script running to show X content f online and X content offline, but I can’t seemt get it working. The code I’m using is:

<?php
$streamChannel = "mastersonothing";  

$json_array = json_decode(file_get_contents("https://api.twitch.tv/kraken/streams.json?channel=$streamChannel"), true);  

if(isset($json_array['streams'][0]['channel'])) {  
echo "<div id='streamonline'>Stream is Online</div>";  
} else {  
echo "<div id='streamoffline'>Stream is Offline</div>";  
}  

?>  

I’ve also tried removing the .jason part:

$json_array = json_decode(file_get_contents("https://api.twitch.tv/kraken/streams/channel=$streamChannel"), true);

With no luck. I’m pretty much a n00b when it comes to PHP though. If anyone could help it would be great appreciated!

1 Like

You’re not sending a Client-ID, which is required to use the API. https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843#.ws4h7qtyp

I wouldn’t recommend using file_get_contents and instead use cURL. There are many threads here on the forum showing how to use cURL and how to detect if someone is online/offline.



Okay, so for example, if my Client ID was 12345678901234

Where do I put the Client-ID in relation to this code?

Would this also be correct before adding the client ID in:

<?php
$streamChannel = "mastersonothing";  
$url = "https://api.twitch.tv/kraken/streams.json?channel=mastersonothing"
$clientid = "12345678901234"

$json_array = json_decode(curl($url), true);  

if(isset($json_array['streams'][0]['channel'])) {  
echo "<div id='streamonline'>Stream is Online</div>";  
} else {  
echo "<div id='streamoffline'>Stream is Offline</div>";  
}  

?>

Thank you in advance!

In your example you define $clientid but never use it.

Also, remove the “.json” from the send of the API URL since that is not the correct syntax.

https://api.twitch.tv/kraken/streams.json?channel=mastersonothing should be https://api.twitch.tv/kraken/streams?channel=mastersonothing

@SPKuja The blog has a full example in PHP that will show you how to add the Client-ID.

1 Like

I’ve foud this on the blog which outlines the need for the Client ID: https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843#.hzfyzk6yc but I cannot for the life of me work out how to integrate the client ID into the code I have.

I read on the Github:

In situations where headers cannot be set, you can also specify a client ID as a querystring parameter: client_id=<client_id>

Which I understand as I can use this in my code: client_id=$clientid but again, I really do not know where this would need to go.

Based on the change George advised, my code now looks like this:

<?php
$streamChannel = "mastersonothing";  
$url = "https://api.twitch.tv/kraken/streams?channel=mastersonothing"
$clientid = "12345678901234"

$json_array = json_decode(curl($url), true);  

if(isset($json_array['streams'][0]['channel'])) {  
echo "<div id='streamonline'>Stream is Online</div>";  
} else {  
echo "<div id='streamoffline'>Stream is Offline</div>";  
}  

?>

If someone could please advise where this client ID needs to go, I would be greatly appreciative. I’ve read the blog post as well as the Github and I don’t understand it. As I mentioned in my original post, this is not my area or expertise. Thanks!

That is exactly it.
Your new url will be:

$newurl = $url . "&client_id=" . $clientid;

This is known as sending the client ID as a parameter.
One can also send it as a header, which is the preferred method, but this works just as well.

Thank you JB940, that makes a little more sense to me. So would I be right in thinking my code should be altered as such:

<?php
$streamChannel = "mastersonothing";  
$url = "https://api.twitch.tv/kraken/streams?channel=mastersonothing"
$clientid = "12345678901234"

$json_array = json_decode(curl($url), true);  

$newurl = $url . "&client_id=" . $clientid;

if(isset($json_array['streams'][0]['channel'])) {  
echo "<div id='streamonline'>Stream is Online</div>";  
} else {  
echo "<div id='streamoffline'>Stream is Offline</div>";  
}  

?>

Or would I replace this section $json_array = json_decode(curl($url), true); with the newurl code?

Sorry if I’m being dense.

Can anyone at all help?

<?php $streamChannel = "mastersonothing"; $url = "https://api.twitch.tv/kraken/streams?channel=" . $streamChannel; $clientID = array( 'Client-ID: 0000000000000000000000000000000' );

function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;

}

$json_array = json_decode(file_get_contents_curl($url), true);

if(isset($json_array[‘streams’][0][‘channel’])) {
echo “

Stream is Online
”;
} else {
echo “
Stream is Offline
”;
}

?>

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