Last active
June 11, 2024 14:32
-
-
Save MineTheCube/034eff1b664b18e98658 to your computer and use it in GitHub Desktop.
PHP functions to convert UUID to/from Minecraft username
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get UUID from Username | |
* | |
* @param string $username | |
* @return string|bool UUID (without dashes) on success, false on failure | |
*/ | |
function username_to_uuid($username) { | |
$profile = username_to_profile($username); | |
if (is_array($profile) and isset($profile['id'])) { | |
return $profile['id']; | |
} | |
return false; | |
} | |
/** | |
* Get Profile (Username and UUID) from username | |
* | |
* @uses http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time | |
* | |
* @param string $username | |
* @return array|bool Array with id and name, false on failure | |
*/ | |
function username_to_profile($username) { | |
if (is_valid_username($username)) { | |
$json = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . $username); | |
if (!empty($json)) { | |
$data = json_decode($json, true); | |
if (is_array($data) and !empty($data)) { | |
return $data; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Get username from UUID | |
* | |
* @uses http://wiki.vg/Mojang_API#UUID_-.3E_Name_history | |
* | |
* @param string $uuid | |
* @return string|bool Username on success, false on failure | |
*/ | |
function uuid_to_username($uuid) { | |
$uuid = minify_uuid($uuid); | |
if (is_string($uuid)) { | |
$json = file_get_contents('https://api.mojang.com/user/profiles/' . $uuid . '/names'); | |
if (!empty($json)) { | |
$data = json_decode($json, true); | |
if (!empty($data) and is_array($data)) { | |
$last = array_pop($data); | |
if (is_array($last) and isset($last['name'])) { | |
return $last['name']; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Check if string is a valid Minecraft username | |
* | |
* @param string $string to check | |
* @return bool Whether username is valid or not | |
*/ | |
function is_valid_username($string) { | |
return is_string($string) and strlen($string) >= 2 and strlen($string) <= 16 and ctype_alnum(str_replace('_', '', $string)); | |
} | |
/** | |
* Remove dashes from UUID | |
* | |
* @param string $uuid | |
* @return string|bool UUID without dashes (32 chars), false on failure | |
*/ | |
function minify_uuid($uuid) { | |
if (is_string($uuid)) { | |
$minified = str_replace('-', '', $uuid); | |
if (strlen($minified) === 32) { | |
return $minified; | |
} | |
} | |
return false; | |
} | |
/** | |
* Add dashes to an UUID | |
* | |
* @param string $uuid | |
* @return string|bool UUID with dashes (36 chars), false on failure | |
*/ | |
function format_uuid($uuid) { | |
$uuid = minify_uuid($uuid); | |
if (is_string($uuid)) { | |
return substr($uuid, 0, 8) . '-' . substr($uuid, 8, 4) . '-' . substr($uuid, 12, 4) . '-' . substr($uuid, 16, 4) . '-' . substr($uuid, 20, 12); | |
} | |
return false; | |
} |
Nice library !
I would have gone with a regex for dashes in uuid like this but substr is ok too :)
Edit : I use this to expand uuid
$uuid = preg_replace('#([a-z0-9]{8})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{12})#', '$1-$2-$3-$4-$5', $uuid);
i cant seem to get it to work and i dont understand it enough to know why... has the mojang api changed for this since it was written?
other things seem to work, but uuid_to_username always returns false.
Well I just tested, the script is working fine:
<?php
require 'mc-api.php';
$uuid = 'e9464e86-daf1-4414-900b-2b08e8710459';
$name = uuid_to_username($uuid);
var_dump($name);
Maybe it's your host? I heard some hosting services like OVH are often blocked by Mojang's API, due to too many requests made by the same host.
Got it working after I posted that. Turned out to be mojang’s API was down that day for several hours. Probably having to do with the 1.16 release and stuff or something. Works great. Using it to put bans up on our website!
———
Kat
… On Jul 14, 2020, at 06:25, MineTheCube ***@***.***> wrote:
***@***.*** commented on this gist.
Well I just tested, the script is working fine:
<?php
require 'mc-api.php';
$uuid = 'e9464e86-daf1-4414-900b-2b08e8710459';
$name = uuid_to_username($uuid);
var_dump($name);
Maybe it's your host? I heard some hosting services like OVH are often blocked by Mojang's API, due to too many requests made by the same host.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, works just fine! 👍