Advertisement
kyroskoh

Twitch CustomAPI - Last Followers

May 7th, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. <?php
  2. // Set the broadcaster name to lowercase.
  3. $BroadcasterLower = strtolower($_GET["Broadcaster"]);
  4. // Set variable of GET data.
  5. $GeneralQuery = $_GET["GeneralQuery"];
  6. // Defaullt fallback in case nothing was entered for how many followers to print.
  7. if (empty($_GET["GeneralQuery"]) || $_GET["GeneralQuery"] == null)
  8. {
  9.     $GeneralQuery = 5;
  10. }
  11. // Check to make sure input is numeric.
  12. if(is_numeric($GeneralQuery))
  13. {
  14.     // More than 1, less than 25 (don't want to run into too long of a string).
  15.     if($GeneralQuery > "25" || $GeneralQuery < "0")
  16.     {
  17.         echo $GeneralQuery . ', is outside of accepted range, 1-25 is the accepted range.';
  18.     }
  19.     // Good to good, valid numeric input between 1-25.
  20.     else
  21.     {
  22.         // Inject lowercase names to get the last followers.
  23.         $ch = curl_init("https://api.twitch.tv/kraken/channels/" . $BroadcasterLower . "/follows?direction=DESC&limit=" . $GeneralQuery . "&offset=0");
  24.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  25.         $result = curl_exec($ch);
  26.         curl_close($ch);
  27.         // Decode the above JSON data.
  28.         $LastFollowersCheckData = json_decode($result, true);
  29.         // Only print the last follower, get the wording right... blah blah blah.
  30.         echo htmlspecialchars($_GET["Broadcaster"]) . "'s last ";
  31.         // Wording for only the last follower.
  32.         if($GeneralQuery == 1)
  33.         {
  34.             echo 'follower is: ';
  35.         }
  36.         else
  37.         {
  38.             // Wording for the last 2-25 followers.
  39.             echo $GeneralQuery . ' followers are: ';
  40.         }
  41.         for ($x=0; $x <= $GeneralQuery; $x++)
  42.         {
  43.             echo $LastFollowersCheckData["follows"][$x]["user"]["display_name"];
  44.             // Die after the last follower, without having a "," at the end of it.
  45.             if($x == ($GeneralQuery - 1))
  46.             {
  47.                 die();
  48.             }
  49.             echo ', ';
  50.         }
  51.     }
  52. }
  53. // Bad input.
  54. else
  55. {
  56.     echo 'You need to input a valid numeric between 1-25';
  57. }
  58. ?>
  59.  
  60. Example: LastFollowers.php?Broadcaster=TwitchBroadcaster&GeneralQuery=25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement