如何使用 Mojang API 和 PHP
How to use the Mojang API with PHP
Mojang API 允许您访问与玩游戏的玩家相关的信息。 API 有一份文档,可以在 here 中找到。但是,由于我在使用 API 方面经验不足,我想知道如何获取玩家的用户名历史记录。在文档中,UUID -> Name history
下有一个 URL、https://api.mojang.com/user/profiles/<uuid>/names
。我知道如何获取播放器的 UUID,通过在 PHP 中使用上面的 URL,我得到了这个输出;
[{"name":"JizzInYaTaco22"},{"name":"_scrunch","changedToAt":1423047892000}]
如何设置此输出的样式以使其仅显示玩家的姓名?这是 link 显示我希望它显示的内容:Link
这是我的代码:
$username 来自单独 php 页面上的表单。
$username = $_POST["username"];
// Get the userinfo
$content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username));
// Decode it
$json = json_decode($content);
// Save the uuid
$uuid = $json->uuid;
var_dump($json, $json->id, $uuid);
// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles' . urlencode($uuid) . '/names');
// Decode it
$json = json_decode($content);
$names = array(); // Create a new array
foreach ($json as $name) {
$names[] = $name->name; // Add each "name" value to our array "names"
}
echo 'UUID: ' . $uuid . '<br />Name history: ' . implode(', ', $names);
数据为JSON.
您可以使用 file_get_contents()
(which will, by default, execute a GET request) or cURL
请求数据,以更高级的方式从 URL 获取数据。
如果用户名中有特殊字符,可以使用 json_decode()
and reading the properties of the object it creates. Also note that I've used urlencode()
进行解码。
<?php
$username = $_POST["username"];
$url = "https://api.mojang.com/users/profiles/minecraft/" . urlencode($username);
$content = file_get_contents($url); // Loads data from an URL
// eg. {"id":"360d11df2b1d41a78e1775df49444128","name":"_scrunch"}
$json = json_decode($content);
print_r($json);
/*
* stdClass Object
* (
* [id] => 360d11df2b1d41a78e1775df49444128
* [name] => _scrunch
* )
*/
var_dump( $json->id ); // string(32) "360d11df2b1d41a78e1775df49444128"
var_dump( $json->name ); // string(8) "_scrunch"
为了提高可读性,让我们稍微更高级一点、更商业一点:
class MojangApi {
const BASE_URL = 'https://api.mojang.com/';
public static function getInstance() {
static $instance;
if ($instance === null) {
$instance = new MojangApi();
}
return $instance;
}
protected function callApi($url) {
$fullUrl = self::BASE_URL . $url;
$rawJson = file_get_contents($url);
return json_decode($rawJson);
}
public function getUserInfo($username) {
return $this->callApi('users/profiles/minecraft/' . urlencode($username));
}
public function getNames($uuid) {
$result = $this->callApi(sprintf('user/profiles/%s/names', urlencode($uuid)));
$names = array();
foreach ($result as $singleResult) {
$names[] = $singleResult->name;
}
return $names;
}
}
用法:
$api = MojangApi::getInstance();
$userInfo = $api->getUserInfo($_POST['username']);
var_dump($userInfo->name); // eg. string(8) "_scrunch"
// ---------------
$usernames =$api->getNames($uuid);
print_r($usernames); // Array ( 'JizzInYaTaco22', '_scrunch' )
如果您需要联系他们 API 的其他部分,您可以使用新方法扩展此 class。只需用 https://api.mojang.com/
.
之后的 URL 调用 $this->callApi()
对于你原来的问题,超级简化:
<?php
// Load the username from somewhere
$username = '_scrunch';
// Get the userinfo
$content = file_get_contents('https://api.mojang.com/user/profiles/minecraft/' . urlencode($username));
// Decode it
$json = json_decode($content);
// Check for error
if (!empty($json->error)) {
die('An error happened: ' . $json->errorMessage);
}
// Save the uuid
$uuid = $json->id;
// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');
// Decode it
$json = json_decode($content);
$names = array(); // Create a new array
foreach ($json as $name) {
$input = $name->name;
if (!empty($name->changedToAt)) {
// Convert to YYYY-MM-DD HH:MM:SS format
$time = date('Y-m-d H:i:s', $name->changedToAt);
$input .= ' (changed at ' . $time . ')';
}
$names[] = $input; // Add each "name" value to our array "names"
}
echo 'UUID: ' . $uuid . '<br />Name history: ' . implode(', ', $names);
Mojang API 允许您访问与玩游戏的玩家相关的信息。 API 有一份文档,可以在 here 中找到。但是,由于我在使用 API 方面经验不足,我想知道如何获取玩家的用户名历史记录。在文档中,UUID -> Name history
下有一个 URL、https://api.mojang.com/user/profiles/<uuid>/names
。我知道如何获取播放器的 UUID,通过在 PHP 中使用上面的 URL,我得到了这个输出;
[{"name":"JizzInYaTaco22"},{"name":"_scrunch","changedToAt":1423047892000}]
如何设置此输出的样式以使其仅显示玩家的姓名?这是 link 显示我希望它显示的内容:Link
这是我的代码:
$username 来自单独 php 页面上的表单。
$username = $_POST["username"];
// Get the userinfo
$content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username));
// Decode it
$json = json_decode($content);
// Save the uuid
$uuid = $json->uuid;
var_dump($json, $json->id, $uuid);
// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles' . urlencode($uuid) . '/names');
// Decode it
$json = json_decode($content);
$names = array(); // Create a new array
foreach ($json as $name) {
$names[] = $name->name; // Add each "name" value to our array "names"
}
echo 'UUID: ' . $uuid . '<br />Name history: ' . implode(', ', $names);
数据为JSON.
您可以使用 file_get_contents()
(which will, by default, execute a GET request) or cURL
请求数据,以更高级的方式从 URL 获取数据。
如果用户名中有特殊字符,可以使用 json_decode()
and reading the properties of the object it creates. Also note that I've used urlencode()
进行解码。
<?php
$username = $_POST["username"];
$url = "https://api.mojang.com/users/profiles/minecraft/" . urlencode($username);
$content = file_get_contents($url); // Loads data from an URL
// eg. {"id":"360d11df2b1d41a78e1775df49444128","name":"_scrunch"}
$json = json_decode($content);
print_r($json);
/*
* stdClass Object
* (
* [id] => 360d11df2b1d41a78e1775df49444128
* [name] => _scrunch
* )
*/
var_dump( $json->id ); // string(32) "360d11df2b1d41a78e1775df49444128"
var_dump( $json->name ); // string(8) "_scrunch"
为了提高可读性,让我们稍微更高级一点、更商业一点:
class MojangApi {
const BASE_URL = 'https://api.mojang.com/';
public static function getInstance() {
static $instance;
if ($instance === null) {
$instance = new MojangApi();
}
return $instance;
}
protected function callApi($url) {
$fullUrl = self::BASE_URL . $url;
$rawJson = file_get_contents($url);
return json_decode($rawJson);
}
public function getUserInfo($username) {
return $this->callApi('users/profiles/minecraft/' . urlencode($username));
}
public function getNames($uuid) {
$result = $this->callApi(sprintf('user/profiles/%s/names', urlencode($uuid)));
$names = array();
foreach ($result as $singleResult) {
$names[] = $singleResult->name;
}
return $names;
}
}
用法:
$api = MojangApi::getInstance();
$userInfo = $api->getUserInfo($_POST['username']);
var_dump($userInfo->name); // eg. string(8) "_scrunch"
// ---------------
$usernames =$api->getNames($uuid);
print_r($usernames); // Array ( 'JizzInYaTaco22', '_scrunch' )
如果您需要联系他们 API 的其他部分,您可以使用新方法扩展此 class。只需用 https://api.mojang.com/
.
$this->callApi()
对于你原来的问题,超级简化:
<?php
// Load the username from somewhere
$username = '_scrunch';
// Get the userinfo
$content = file_get_contents('https://api.mojang.com/user/profiles/minecraft/' . urlencode($username));
// Decode it
$json = json_decode($content);
// Check for error
if (!empty($json->error)) {
die('An error happened: ' . $json->errorMessage);
}
// Save the uuid
$uuid = $json->id;
// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');
// Decode it
$json = json_decode($content);
$names = array(); // Create a new array
foreach ($json as $name) {
$input = $name->name;
if (!empty($name->changedToAt)) {
// Convert to YYYY-MM-DD HH:MM:SS format
$time = date('Y-m-d H:i:s', $name->changedToAt);
$input .= ' (changed at ' . $time . ')';
}
$names[] = $input; // Add each "name" value to our array "names"
}
echo 'UUID: ' . $uuid . '<br />Name history: ' . implode(', ', $names);