如何在PHP中使用Mojang API

英国威廉·威廉姆斯

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

数据是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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在PHP中使用CURL调用REST API?

来自分类Dev

我想知道如何在PHP中使用短信API

来自分类Dev

如何在REST API中使用会话

来自分类Dev

如何在FireMonkey中使用REST API?

来自分类Dev

如何在Dreamfactory中使用Mailgun API

来自分类Dev

如何在Python中使用linkedin API

来自分类Dev

如何在Asana API中使用JSONP

来自分类Dev

如何在Spring API中使用umlaut

来自分类Dev

如何在Android中使用Wikipedia API

来自分类Dev

如何在RESTHeart API中使用limit()?

来自分类Dev

如何在FireMonkey中使用REST API?

来自分类Dev

如何在Asana API中使用JSONP

来自分类Dev

如何在Dreamfactory中使用Mailgun API

来自分类Dev

如何在Swift中使用CoreAudio API

来自分类Dev

如何在RESTHeart API中使用limit()?

来自分类Dev

如何在 soundcloud 中使用 oembed API?

来自分类Dev

如何在 MessagePack 中使用 Primitive API?

来自分类Dev

如何在PHP中使用StackOverflow API V 2.2提出问题

来自分类Dev

如何在Python应用程序中使用api-sdk(用PHP编写)

来自分类Dev

如何在Facebook的SDK中使用图形api访问令牌在php中

来自分类Dev

如何在PHP中使用Google Map API获取已创建多边形的纬度,经度

来自分类Dev

如何在WePay API中使用PHP CURL传递JSON数据?

来自分类Dev

如何在 PHP 中使用 youtube v3 api 获取视频统计信息

来自分类Dev

如何在 JSON API 的 PHP 中使用 Curl 返回一些变量

来自分类Dev

如何在 php 中使用 Gmail API 发送电子邮件

来自分类Dev

如何在word api API中使用range.expand

来自分类Dev

API使用的是CORS,如何在vue中使用axios?

来自分类Dev

如何在Node.js中使用XHR API?

来自分类Dev

如何在Browserify中使用API的--node选项

Related 相关文章

热门标签

归档