Checking if a channel is live and streaming on Twitch requires a client id, you can get your client id from here.
The endpoint for getting a streams information is get_streams, Here i will be using the user_login parameter to specify which channel we want information for, otherwise you can use user_id but you will have to find that for the streamer.
Making a call with CURL in PHP looks like this:
$client_id = 'PUTCLIENTIDHERE';//Twitch client id AKA: api key
$user = 'shroud';//Streamers username
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=$user");//Endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"//Add auth
));
$profile_data = json_decode(curl_exec($ch), true);
curl_close($ch);If the user isnt live then no data will be returned, otherwise you will get 11 response fields with data as seen at get_streams.
Simple check if channel is live:
if (!isset($profile_data['data'][0])) {
$live = 0;//Not live
} else {
$live = 1;//Is live
}Since we now know the channel is live (or not) we can fetch data avoiding errors for when the channel isnt live and there is no returned values:
if ($live == 1) {
$title = $profile_data['data'][0]['title'];
$viewer_count = $profile_data['data'][0]['viewer_count'];
$game_id = $profile_data['data'][0]['game_id'];
....The returned data from get_streams does not show the game name, just its Twitch assigned id. Getting the game name and image involves the games endpoint, just pass the game_id into ?id=
Here the code gets the streaming game name and the artwork/image:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/games?id=$game_id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"
));
$game_data = json_decode(curl_exec($ch), true);
curl_close($ch);
$game_name = $game_data['data'][0]['name'];
$game_artwork = $game_data['data'][0]['box_art_url'];getting the time went live ago in hours minutes involves some converting, formatting and date_diff():
$went_live_at = DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:s", strtotime($profile_data['data'][0]['started_at'])))->format('Y-m-d H:i:s');
$started = date_create($went_live_at);
$now = date_create(date('Y-m-d H:i:s'));
$diff = date_diff($started, $now);
$hours = $diff->h;
$minutes = $diff->i;If streamer is live output a string with some data else state not live:
echo "$user is playing $game_name, started streaming $hours hours $minutes minutes ago and has $viewer_count viewers TITLE: $title";
} else {
echo "$user is not live";
}
<?php
date_default_timezone_set('UTC');
$client_id = 'S3Cr3Tc0d3';
$user = 'shroud';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=$user");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"
));
$profile_data = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!isset($profile_data['data'][0])) {
$live = 0;
} else {
$live = 1;
}
if ($live == 1) {
$title = $profile_data['data'][0]['title'];
$viewer_count = $profile_data['data'][0]['viewer_count'];
$game_id = $profile_data['data'][0]['game_id'];
$went_live_at = DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:s", strtotime($profile_data['data'][0]['started_at'])))->format('Y-m-d H:i:s');
$started = date_create($went_live_at);
$now = date_create(date('Y-m-d H:i:s'));
$diff = date_diff($started, $now);
$hours = $diff->h;
$minutes = $diff->i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/games?id=$game_id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"
));
$game_data = json_decode(curl_exec($ch), true);
curl_close($ch);
$game_name = $game_data['data'][0]['name'];
$game_artwork = $game_data['data'][0]['box_art_url'];
echo "$user is playing $game_name, started streaming $hours hours $minutes minutes ago and has $viewer_count viewers TITLE: $title";
} else {
echo "$user is not live";
}Another variant which uses functions:
<?php
date_default_timezone_set('UTC');
$client_id = 'mYC0d3h3r3';
$user = 'shroud';
function user_stream($client_id, $user)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=$user");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"
));
$profile_data = json_decode(curl_exec($ch), true);
curl_close($ch);
return $profile_data;
}
function game_data($client_id, $game_id)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/games?id=$game_id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Client-ID: $client_id"
));
$game_data = json_decode(curl_exec($ch), true);
curl_close($ch);
return $game_data;
}
function is_live($data)
{
if (!isset($data['data'][0])) {
return false;
} else {
return true;
}
}
$profile_data = user_stream($client_id, $user);
if (is_live($profile_data)) {
$title = $profile_data['data'][0]['title'];
$viewer_count = $profile_data['data'][0]['viewer_count'];
$game_id = $profile_data['data'][0]['game_id'];
$went_live_at = DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d H:i:s", strtotime($profile_data['data'][0]['started_at'])))->format('Y-m-d H:i:s');
$started = date_create($went_live_at);
$now = date_create(date('Y-m-d H:i:s'));
$diff = date_diff($started, $now);
$hours = $diff->h;
$minutes = $diff->i;
$game_data = game_data($client_id, $game_id);
$game_name = $game_data['data'][0]['name'];
$game_artwork = $game_data['data'][0]['box_art_url'];
echo "$user is playing $game_name, started streaming $hours hours $minutes minutes ago and has $viewer_count viewers TITLE: $title";
} else {
echo "$user is not live";
}