mirror of
https://github.com/elyby/mojang-api.git
synced 2025-05-31 14:11:44 +05:30
Implemented Playernames to UUIDs endpoint
This commit is contained in:
45
src/Api.php
45
src/Api.php
@@ -12,6 +12,7 @@ use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Uri;
|
||||
use InvalidArgumentException;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class Api {
|
||||
@@ -132,6 +133,50 @@ class Api {
|
||||
return $this->uuidToTextures($this->usernameToUUID($username)->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $names list of users' names
|
||||
*
|
||||
* @return \Ely\Mojang\Response\ProfileInfo[] response array is indexed with the initial username case
|
||||
*
|
||||
* @throws \Ely\Mojang\Exception\MojangApiException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*
|
||||
* @url https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs
|
||||
*/
|
||||
public function playernamesToUuids(array $names): array {
|
||||
foreach ($names as $i => $name) {
|
||||
if (empty($name)) {
|
||||
unset($names[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($names) > 100) {
|
||||
throw new InvalidArgumentException('You cannot request more than 100 names per request');
|
||||
}
|
||||
|
||||
$response = $this->getClient()->request('POST', 'https://authserver.mojang.com/authenticate', [
|
||||
'json' => array_values($names),
|
||||
]);
|
||||
$body = $this->decode($response->getBody()->getContents());
|
||||
|
||||
$result = [];
|
||||
foreach ($body as $record) {
|
||||
$object = Response\ProfileInfo::createFromResponse($record);
|
||||
$key = $object->getName();
|
||||
foreach ($names as $i => $name) {
|
||||
if (mb_strtolower($name) === mb_strtolower($object->getName())) {
|
||||
unset($names[$i]);
|
||||
$key = $name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$result[$key] = $object;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $login
|
||||
* @param string $password
|
||||
|
||||
Reference in New Issue
Block a user