Skip to content

Instantly share code, notes, and snippets.

@LukeXF
Last active August 29, 2015 14:14
Show Gist options
  • Save LukeXF/c262e05471fafd91ae31 to your computer and use it in GitHub Desktop.
Save LukeXF/c262e05471fafd91ae31 to your computer and use it in GitHub Desktop.
api.luke.sx examples

The Web API system - api.luke.sx

Minecraft UUID:

  • This you can be used to view the uptime of all Mojang related servers. I have combined this with the server checker to add a more diverse error reporting in some of my projects. Quite useful if you wish to see what is on fire at Mojang.

Minecraft Skin Viewer:

  • For including a visual representation of the user on forums and leaderboards of the players skin. Sizable from 20-250px for head, 70-250px for skins for maximum flexibility.

Minecraft Server Query:

  • Usually I would use the status to return a visual icon or color to represent if there server is online, other useful features is ping and server count.

Mojang Uptime Checker:

  • This you can be used to view the uptime of all Mojang related servers. I have combined this with the server checker to add a more diverse error reporting in some of my projects. Quite useful if you wish to see what is on fire at Mojang.

Service Ping Checker:

  • Allows you to ping both websites and Minecraft servers with timeout and offline detection for both services via the status. Returns the ping, Be wary, any cloudflare hosted site will return 1 responce if online.
<!-- include stylesheet for status icon -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<?php
// edit this variable to change the queried server
$my_server = "play.nirvanamc.com";
// function added to convert object to array
// this is not needed if you already have a similar function
// or wish to use the decoded data as an object
function objectToArray($d) {
// convert object to array
if (is_object($d)) { $d = get_object_vars($d); }
// converts array to sexy array
if (is_array($d)) { return array_map(__FUNCTION__, $d); }
// else assume it is an array
else { return $d; }
}
// load the json web file
$json_file = 'http://api.luke.sx/query/' . $my_server;
// load file into string
$json_string = file_get_contents($json_file);
// decode json string
$decoded_object = json_decode($json_string);
// use objectToArray function to convert object to array
$final_array = objectToArray($decoded_object);
// check if the server is online
if ($final_array['status'] == false) {
echo "<i class='fa fa-times'></i> <b>" . $my_server . "</b> seems to be offline right now.";
} else {
// keep grammar of plural in players correct
if ($final_array['players']['online'] == 0) {
// if no players are online
$players = "no";
} elseif ($final_array['players']['online'] == 1) {
// if there is one player online
$players = "<b>" . $final_array['players']['online'] . "</b> player";
} else {
// else assume this, if there is more than one player online
$players = "<b>" . $final_array['players']['online'] . "</b> players";
}
// output a beautiful formatted array
echo "<pre>";
print_r($final_array);
echo "</pre>";
// or feel free to output specific values of the array
echo "<i class='fa fa-check'></i> <b>" . $my_server . "</b> is currently online.
With " . $players . " on.";
}
// code example by Luke Brown (http://luke.sx)
?>
<!--
No need for PHP here! We can handle images all
via HTML.
-->
<style type="text/css">
/* adds some simple styling to the images */
img {
border-radius: 5px;
padding: 10px;
display: inline-table;
}
</style>
<img src="http://api.luke.sx/head/DiamondXF/250" alt="DiamondXF's head">
<img src="http://api.luke.sx/head/DiamondXF" alt="DiamondXF's head">
<img src="http://api.luke.sx/skin/DiamondXF/100" alt="DiamondXF's head">
<img src="http://api.luke.sx/skin/DiamondXF" alt="DiamondXF's head">
<!-- code example by Luke Brown (http://luke.sx) -->
<!--
Dynamically load in player heads after page load,
avoiding long waiting time for the page to load in
the heads. Everything in player-heads is counted by
the script that is labeled player-head, enjoy!
-->
<div class="player-heads">
<div class="player-head" id="DiamondXF"></div>
<div class="player-head" id="MrSuperSweet"></div>
<div class="player-head" id="Toxicant7"></div>
<div class="player-head" id="Spawl"></div>
<div class="player-head" id="Slashki"></div>
<div class="player-head" id="Xephale"></div>
<div class="player-head" id="Fight_Or_Die"></div>
</div>
<style type="text/css">
/* styling potions the images in line with a curved edge */
.player-head {
padding: 10px;
display: inline-table;
}
.player-head img {
border-radius: 5px;
}
</style>
<script type="text/javascript">
// Create the function
$(window).load(function() {
// laod each player head into the function
$(".player-head").each(function(head, name) {
// load the username by grabbing the id.
var username = $(name).attr('id');
// insert into the current div the image with the name
$(this).html("<img src='//api.luke.sx/head/" + username + "/100' />");
// magic is over!
});
});
/* code example by Luke Brown (http://luke.sx) */
</script>
<span>full example over<a href="http://codepen.io/LukeXF/pen/MYKWaQ" target="_blank">here</a>.</span>
<!-- include stylesheet for status icon -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<?php
// warning, some hot stuff here :)
// function added to convert object to array
// this is not needed if you already have a similar function
// or wish to use the decoded data as an object
function objectToArray($d) {
// convert object to array
if (is_object($d)) { $d = get_object_vars($d); }
// converts array to sexy array
if (is_array($d)) { return array_map(__FUNCTION__, $d); }
// else assume it is an array
else { return $d; }
}
// load the json web file
$json_file = 'http://api.luke.sx/status';
// load file into string
$json_string = file_get_contents($json_file);
// decode json string
$decoded_object = json_decode($json_string);
// use objectToArray function to convert object to array
$final_array = objectToArray($decoded_object);
// we're going to create a new array that is easy to understand
// for the key services only
$sexy_array = array(
'login' => $final_array[2]['account.mojang.com'],
'session' => $final_array[1]['session.minecraft.net'],
'website' => $final_array[0]['minecraft.net'],
'skins' => $final_array[4]['skins.minecraft.net'],
'auth' => $final_array[5]['authserver.mojang.com']
);
// loop through the final array for each status
foreach ($sexy_array as $service => $status){
if ($status == "green") {
// if the service is online
echo "<i class='fa fa-check'></i> " . $service . " servers are up and running fine.<br>";
} elseif ($status == "yellow") {
// if the service is kinda online
echo "<i class='fa fa-minus'></i> " . $service . " servers are having a bad day, it's a bit shaky<br>";
} else {
// if the service is offline
echo "<i class='fa fa-times'></i> " . $service . " servers are offline like usually... great.<br>";
}
}
// code example by Luke Brown (http://luke.sx)
?>
<?php
// edit this variable to change the queried Minecraft uuid
$my_uuid = "8f65bf20-b3e6-45ff-b67b-a14a9a54e7b6";
// edit this variable to change the queried Minecraft username
$my_user = "DiamondXF";
// function added to convert object to array
// this is not needed if you already have a similar function
// or wish to use the decoded data as an object
function objectToArray($d) {
// convert object to array
if (is_object($d)) { $d = get_object_vars($d); }
// converts array to sexy array
if (is_array($d)) { return array_map(__FUNCTION__, $d); }
// else assume it is an array
else { return $d; }
}
// function to convert the uuid to a username
function uuidToUser($uuid) {
// load the json web file
$json_file = 'http://api.luke.sx/uuid/' . $uuid;
// load file into string
$json_string = file_get_contents($json_file);
// decode json string
$decoded_object = json_decode($json_string);
// use objectToArray function to convert object to array
$final_array = objectToArray($decoded_object);
// return the users name as part of the array
return $final_array['name'];
}
// function to convert the username to a uuid
function userToUuid($name) {
// load the json web file
$json_file = 'http://api.luke.sx/user/' . $name;
// load file into string
$json_string = file_get_contents($json_file);
// decode json string
$decoded_object = json_decode($json_string);
// use objectToArray function to convert object to array
$final_array = objectToArray($decoded_object);
// return the users name as part of the array
return $final_array['full_uuid'];
}
// using the uuidToUser function, echo this anywhere
// will echo the username of $my_uuid or whatever
// variable you define inside of the function
echo uuidToUser($my_uuid);
// using the userToUuid function, echo this anywhere
// will echo the uuid of $my_user or whatever
// variable you define inside of the function
echo userToUuid($my_user);
// code example by Luke Brown (http://luke.sx)
?>
try {
URL url = new URL("http://api.luke.sx/webping/google.com");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String rawjson = in.readLine();
JSONObject object = (JSONObject) new JSONParser().parse(rawjson);
//This will return the host of the server you've pinged.
String host = (String) object.get("host");
//This will return the online status of the server. Returns true if online, false if the ping has failed.
boolean online = (boolean) object.get("status");
//The response time is the time in milliseconds that the server takes to get a ping response from the server.
double responsetime = (double) object.get("response-time");
} catch (IOException | ParseException e) {
e.printStackTrace();
}
// code example by Spawl (http://twitter.com/spawl_)
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function webping(serverip){
// load JSON-encoded data from the server using a get http request.
$.getJSON('http://api.luke.sx/webping/' + serverip, function(json){
// if the status is 0, and therefor invalid ip or domain
if (json.status == 0) {
// override id with error message
$('#love').html("oh dear, <b>" + serverip + "</b> seems to be offline or invalid.");
} else {
// return a beautiful response message
$("#love").html("sweet, <b>" + serverip + "</b> is online with a <b>" + json['response-time'] + "</b> response time.");
}
});
}
// edit this variable to change the queired domain or ip
webping('google.com');
</script>
<span id="love"></span>
<!-- code example by Luke Brown (http://luke.sx) -->
<?php
// edit this variable to change the queried domain or ip
$my_ip = "google.com";
// function added to convert object to array
// this is not needed if you already have a similar function
// or wish to use the decoded data as an object
function objectToArray($d) {
// convert object to array
if (is_object($d)) { $d = get_object_vars($d); }
// converts array to sexy array
if (is_array($d)) { return array_map(__FUNCTION__, $d); }
// else assume it is an array
else { return $d; }
}
// load the json web file
$json_file = 'http://api.luke.sx/webping/' . $my_ip;
// load file into string
$json_string = file_get_contents($json_file);
// decode json string
$decoded_object = json_decode($json_string);
// use objectToArray function to convert object to array
$final_array = objectToArray($decoded_object);
// check if the domain or ip is valid
if ($final_array['status'] == 0) {
echo "oh dear, <b>" . $my_ip . "</b> seems to be offline or invalid.";
} else {
// output a beatiful formatted array
echo "<pre>";
print_r($final_array);
echo "</pre>";
// or feel free to output specific values of the array
echo "The repsonse time of <b>" . $my_ip . "</b> is <b>" . $final_array['response-time'] . "</b>.";
}
// code example by Luke Brown (http://luke.sx)
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment