DavidNorgren

Untitled

Mar 31st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. <?
  2.     // http://wiki.vg/Mojang_API
  3.  
  4.     $USERNAME = $_GET["username"];
  5.  
  6.     if (!isset($USERNAME))
  7.         die("Username not set");
  8.  
  9.     $CACHED_SKIN = $_SERVER["DOCUMENT_ROOT"] . "/skincache/$USERNAME.png";
  10.  
  11.     // Get username UUID
  12.     $curl = curl_init();
  13.     curl_setopt_array($curl, array(
  14.         CURLOPT_RETURNTRANSFER => true,
  15.         CURLOPT_URL => "https://api.mojang.com/users/profiles/minecraft/$USERNAME",
  16.         CURLOPT_USERAGENT => "Mine-imator",
  17.         CURLOPT_CONNECTTIMEOUT => 10
  18.     ));
  19.  
  20.     $response = curl_exec($curl);
  21.     $response_parsed = json_decode($response);
  22.  
  23.     if (json_last_error() != JSON_ERROR_NONE)
  24.         die("Could not find user");
  25.  
  26.     if (isset($response_parsed->{"error"}))
  27.     {
  28.         // Fetch from cache or throw error
  29.         if ($response_parsed->{"error"} == "TooManyRequestsException" && file_exists($CACHED_SKIN))
  30.         {
  31.             header("Content-type: image/png");
  32.             header("Content-Length: " . filesize($CACHED_SKIN));
  33.             readfile($CACHED_SKIN);
  34.             die("");
  35.         }
  36.         else
  37.             die("UUID " . $response_parsed->{"error"} . ": " . $response_parsed->{"errorMessage"});
  38.     }
  39.  
  40.     $uuid = $response_parsed->{"id"};
  41.  
  42.     // Get skin URL
  43.     curl_setopt_array($curl, array(
  44.         CURLOPT_RETURNTRANSFER => true,
  45.         CURLOPT_URL => "https://sessionserver.mojang.com/session/minecraft/profile/" . $uuid,
  46.         CURLOPT_USERAGENT => "Mine-imator",
  47.         CURLOPT_CONNECTTIMEOUT => 10
  48.     ));
  49.  
  50.     $response = curl_exec($curl);
  51.     $response_parsed = json_decode($response);
  52.    
  53.     if (json_last_error() != JSON_ERROR_NONE)
  54.         die("Could not find profile");
  55.  
  56.     if (isset($response_parsed->{"error"}))
  57.     {
  58.         // Fetch from cache or throw error
  59.         if ($response_parsed->{"error"} == "TooManyRequestsException" && file_exists($CACHED_SKIN))
  60.         {
  61.             header("Content-type: image/png");
  62.             header("Content-Length: " . filesize($CACHED_SKIN));
  63.             readfile($CACHED_SKIN);
  64.             die("");
  65.         }
  66.         else
  67.             die("Profile " . $response_parsed->{"error"} . ": " . $response_parsed->{"errorMessage"});
  68.     }
  69.  
  70.     $textures_data = $response_parsed->{"properties"}[0]->{"value"};
  71.     $textures_parsed = json_decode(base64_decode($textures_data));
  72.    
  73.     if (json_last_error() != JSON_ERROR_NONE)
  74.         die("Could not find texture data");
  75.  
  76.     $skin_url = $textures_parsed->{"textures"}->{"SKIN"}->{"url"};
  77.    
  78.     // Get skin image
  79.     curl_setopt_array($curl, array(
  80.         CURLOPT_RETURNTRANSFER => true,
  81.         CURLOPT_URL => $skin_url,
  82.         CURLOPT_USERAGENT => "Mine-imator",
  83.         CURLOPT_CONNECTTIMEOUT => 10
  84.     ));
  85.    
  86.     $data = curl_exec($curl);
  87.     curl_close($curl);
  88.  
  89.     // Store in cache
  90.     file_put_contents($CACHED_SKIN, $data);
  91.  
  92.     // Output skin
  93.     header("Content-type: image/png");
  94.     echo $data;
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment