SPKuja
September 20, 2016, 7:37pm
1
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.
hi ! I wrote this script but I get nothing after “bio” … but why ?
<?php
$url = 'https://api.twitch.tv/kraken/users/gronkh';
$json = file_get_contents($url);
$data = json_decode($json, true);
$bio = $data['bio'];
echo "bio: $bio";
?>
whats wrong ?
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");
// T…
Hi friends.
I’m on page 20 output channels, their headers.
The page is loaded for a long time to solve the problem?
Example:Check stream status
<?php
$start = microtime(true);
$channel = "versuta";
$json_file = @file_get_contents("https://api.twitch.tv/kraken/streams/{$channel}", 0, null, null);
$json_array = json_decode($json_file, true);
if ($json_array['stream'] == null)
{echo 'offline' } else { echo 'online' }
$time = microtime(true) - $start;
printf('Time of loading %.4F …
SPKuja
September 20, 2016, 9:18pm
3
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!
george
September 20, 2016, 9:37pm
4
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
SPKuja
September 21, 2016, 7:42am
6
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!
JB940
September 21, 2016, 10:15am
7
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.
SPKuja
September 21, 2016, 10:34am
8
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.
system
Closed
October 22, 2016, 10:08pm
11
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.