Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Last active December 4, 2020 07:17
Show Gist options
  • Save nikoheikkila/8686164 to your computer and use it in GitHub Desktop.
Save nikoheikkila/8686164 to your computer and use it in GitHub Desktop.
PHP: Check from Twitch.tv APIv3 if channel is live
<?php
/**
* Use Twitch.tv API v3 to check if the specified channel has stream going on
*
* @author Niko Heikkilä, 2014
* @param STRING $channel Representing the channel/account name
* @return BOOL TRUE if online, FALSE if offline
*/
function is_channel_live( $channel )
{
$request = json_decode( @file_get_contents( 'https://api.twitch.tv/kraken/streams/' . $channel ) );
return ( ! is_null( $request->stream ) ) ? TRUE : FALSE;
}
?>
@shadowcoinx
Copy link

Hi! How i can add e.g image if channel is online and another image if channel is offline?

And just to clarify will i replace $channel with channel name

@SrJoosh
Copy link

SrJoosh commented Dec 8, 2015

To add the image create a check, like
if(is_channel_live("pewdiepie")){
echo "";
}else{
echo "";
}

And no, That is a method which you would call using $channel as a parameter. to get a true or false response you would just call
is_channel_live("channelname");
and that would return TRUE or FALSE

@nikoheikkila
Copy link
Author

@SrJoosh and @shadowcoinx:

Yep, I'd modify this script to be checked via AJAX (it just needs to echo back something useful for XHR request) and then add the desired elements with Javascript (or jQuery, if that's your thing) to the page's HTML. There could be a interval/repeat function or a refresh button that would launch the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment