Skip to content

Instantly share code, notes, and snippets.

@MineTheCube
Last active August 9, 2023 08:40
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MineTheCube/034eff1b664b18e98658 to your computer and use it in GitHub Desktop.
Save MineTheCube/034eff1b664b18e98658 to your computer and use it in GitHub Desktop.
PHP functions to convert UUID to/from Minecraft username
<?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;
}
@AlbertMN
Copy link

Thank you, works just fine! 👍

@oblx
Copy link

oblx commented Sep 23, 2017

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);

@Mrs-Feathers
Copy link

Mrs-Feathers commented Jun 30, 2020

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.

@MineTheCube
Copy link
Author

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);

image

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.

@Mrs-Feathers
Copy link

Mrs-Feathers commented Jul 14, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment