Mojang API允许您访问与玩游戏的玩家有关的信息。有关该API的文档,可以在这里找到。但是,由于我对使用API并不熟悉,所以我想知道如何获取播放器的用户名历史记录。在“文档”下,UUID -> Name history
有一个URL https://api.mojang.com/user/profiles/<uuid>/names
。我知道如何获取播放器的UUID,并通过在PHP中使用上述URL,获得此输出;
[{"name":"JizzInYaTaco22"},{"name":"_scrunch","changedToAt":1423047892000}]
如何设置此输出的样式,使其仅显示播放器的名称?这是一个显示我想要显示的链接:链接
这是我的代码:
$ 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()
(默认情况下将执行GET请求)请求数据,也可以使用cURL
更高级的方法从URL获取数据。
可以使用json_decode()
并读取其创建的对象的属性来完成解码。另请注意,我使用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的其他部分,则可以使用新方法扩展此类。只需$this->callApi()
使用之后的URL进行调用https://api.mojang.com/
。
对于您的原始问题,超级简化:
<?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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句