getTwitch.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. include_once('includes/db_connect.php');
  3. include_once('includes/global_functions.php');
  4. $radioid = $_GET['radioid'];
  5. $radiomac = $_GET['radiomac'];
  6. if($radioid == "" && $radiomac == ""){
  7. print(json_encode([error => "Not a valid radioid"]));
  8. die();
  9. }else{
  10. if($radiomac != ""){
  11. $radiomac = str_replace(':', '', $radiomac);
  12. $radioid = substr($radiomac, 6 , 11);
  13. }
  14. $currentStream = null;
  15. if($stmt = $mysqli -> prepare("SELECT username FROM Twitch LEFT JOIN users ON Twitch.uid = users.uid WHERE users.radiotoken = ?")){
  16. $stmt -> bind_param("s", $radioid);
  17. $stmt -> execute();
  18. $stmt -> bind_result($TwitchUsername);
  19. $stmt -> fetch();
  20. $stmt -> close();
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/users/".$TwitchUsername."/follows/channels");
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24. $jsonreturn = json_decode(curl_exec($ch), true);
  25. for($x = 0; $x < count($jsonreturn["follows"]); $x++)
  26. {
  27. $channel = $jsonreturn["follows"][$x]["channel"]["display_name"];
  28. curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/streams/".$channel);
  29. $stream = json_decode(curl_exec($ch), true);
  30. if($stream["stream"] != null)
  31. {
  32. $currentStream = array();
  33. $status = $stream["stream"]["channel"]["status"];
  34. $game = $stream["stream"]["game"];
  35. $datetime = $stream["stream"]["created_at"];
  36. $date = strtotime($datetime);
  37. array_push($currentStream, array( Name => $channel,
  38. Title => $status,
  39. Game => $game,
  40. Date => $date));
  41. }
  42. }
  43. }
  44. print(json_encode($currentStream, JSON_UNESCAPED_SLASHES));
  45. }