Last active
December 4, 2020 07:17
-
-
Save nikoheikkila/8686164 to your computer and use it in GitHub Desktop.
PHP: Check from Twitch.tv APIv3 if channel is live
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
@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
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