From e8658fe1cbb0fb8fcecc3fd27e721ad6e7b586ab Mon Sep 17 00:00:00 2001 From: MadMakz Date: Mon, 7 Jan 2013 14:11:08 +0100 Subject: [PATCH] Added simple PHP TwitchStatus script --- PHP/Twitch/twitchstatus.php | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 PHP/Twitch/twitchstatus.php diff --git a/PHP/Twitch/twitchstatus.php b/PHP/Twitch/twitchstatus.php new file mode 100644 index 0000000..96ac07a --- /dev/null +++ b/PHP/Twitch/twitchstatus.php @@ -0,0 +1,95 @@ +. + * + */ + +error_reporting(0); + +if(isset($_GET["channel"])){ + $name = $_GET["channel"]; +} +else{ + $name = "twitch"; +} + + +$stream = "https://api.twitch.tv/kraken/streams/".$name; +$stream = json_decode(file_get_contents_curl($stream), true); +$account = "https://api.twitch.tv/kraken/channels/".$name; +$account = json_decode(file_get_contents_curl($account), true); + +if(!$account["display_name"]){ + exit("Unknown channel."); +} + +$txt = "".$account["display_name"]."

"; +if($stream["stream"] != null){ + $boxart = "http://static-cdn.jtvnw.net/ttv-boxart/".$stream["stream"]["game"].".jpg"; + $txt .= "Live!

"; + $txt .= "Playing: ".$stream["stream"]["game"]."
"; + $txt .= "Viewers: ".$stream["stream"]["viewers"]."

"; +} +else{ + $boxart = "http://img7.imageshack.us/img7/9652/offlineeu.jpg"; + $txt .= "Offline.
"; +} +$txt .= "
Go to Channel"; + +$return = " + + + + + + +
+ ".$txt." + + \"\" +
"; + +echo $return; +exit; + + +/* FUNCTIONS */ +function file_get_contents_curl($url, $agent = "My Agent", $cookie = false, $post = false){ + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); + curl_setopt($ch, CURLOPT_TIMEOUT, 2); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, $agent); + if($cookie){ + curl_setopt($ch, CURLOPT_COOKIESESSION, true); + curl_setopt($ch, CURLOPT_COOKIE, $cookie); + } + curl_setopt($ch, CURLOPT_URL, $url); + if($post){ + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + } + + $data = curl_exec($ch); + curl_close($ch); + + return $data; +} +?> \ No newline at end of file