如何在PHP中使用Mojang API
Mojang API允许您访问与玩游戏的玩家相关的信息。可以找到API的文档。然而,由于我对使用API没有太多经验,我想知道如何获取玩家的用户名历史记录。在文档中,在如何在PHP中使用Mojang API,php,html,css,api,Php,Html,Css,Api,Mojang API允许您访问与玩游戏的玩家相关的信息。可以找到API的文档。然而,由于我对使用API没有太多经验,我想知道如何获取玩家的用户名历史记录。在文档中,在UUID->Name history下有一个URL,https://api.mojang.com/user/profiles//names。我知道如何获取播放器的UUID,通过在PHP中使用上面的URL,我得到了这个输出 如何设置此输出的样式,使其仅显示播放器的名称?这里有一个链接,显示我希望它显示的内容: 这是我的密码: $u
UUID->Name history
下有一个URL,https://api.mojang.com/user/profiles//names
。我知道如何获取播放器的UUID,通过在PHP中使用上面的URL,我得到了这个输出
如何设置此输出的样式,使其仅显示播放器的名称?这里有一个链接,显示我希望它显示的内容: 这是我的密码: $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);
$username=$\u POST[“username”];
//获取用户信息
$content=file\u get\u contents('https://api.mojang.com/users/profiles/minecraft/'.urlencode($username));
//破译
$json=json_解码($content);
//保存uuid
$uuid=$json->uuid;
var_dump($json,$json->id,$uuid);
//获取历史记录(使用$json->uuid)
$content=file\u get\u contents('https://api.mojang.com/user/profiles“.urlencode($uuid)。”/names');
//破译
$json=json_解码($content);
$names=array();//创建一个新数组
foreach($json作为$name){
$names[]=$name->name;//将每个“name”值添加到数组“names”
}
回显“UUID:”$乌伊德。”
名称历史记录:'。内爆(“,”,$names);
数据是JSON
您可以使用(默认情况下,将执行GET请求)请求数据,或者使用更高级的方式从URL获取数据
解码可以通过使用和读取它创建的对象的属性来完成。还请注意,我在用户名中使用了一些特殊字符
<?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"
用法:
$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的其他部分,可以使用新方法扩展此类。只需使用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);
这是JSON。您可以使用$names=json\u decode($json)
获取一个对象,然后使用foreach($names as$name){echo$name->name;}
。啊,我明白了!JSON!所以,我要用我的代码更新我的问题,同样,因为我不知道如何使用API,你能告诉我如何使用吗?我不认为这是使用API,而是“如何从外部URL加载JSON”。一个简单的谷歌搜索应该会创造奇迹。将其视为多个任务,而不是一个大任务。您已经成功加载了数据(“如何加载url php”)。现在需要对其进行解码(“如何解码json php”)并对其进行迭代(“如何获取json值php”)。我在第一篇评论中发布的代码包括这两个方面。只需将json\u decode($json)
切换为json\u decode($uuid)
。我感谢您的所有帮助:)但我似乎对foreach
有问题。我收到此错误,为foreach()提供的参数无效。
。我已经更新了问题中的代码。是的,我的错-我不知道你从未加载数据。如果你核对一下我的答案,你应该能弄明白。如果您有什么需要解释的,请告诉我。谢谢您的帮助!这段代码似乎是通过stdClass对象([id]=>360d11df2b1d41a78e1775df49444128[name]=>\u scrunch)字符串(32)“360d11df2b1d41a78e1775df49444128”字符串(8)”\u scrunch“
@jlwilliamsuk这将是print\u r
和var\u dump
函数来输出的。您也可以查看我的更为“商业化”的解决方案,它可以扩展到您的内心内容。考虑<代码> PrimtJrr < /C>和字符串(8)
。在此之后,我可以简单地将信息添加到一个变量中吗?该变量仍然来自var\u dump
——如果不需要,只需将它们全部删除即可。我还更新了答案,以反映您原来的问题,并向您展示如何扩展课程。对我来说,这似乎是一件很难理解的事情!不过,谢谢你的帮助!但是,我希望只显示播放器的名称历史记录,而不显示JSON代码。这里有一个网站可以做到这一点:只要输入“\u scrunch”,你就会得到该玩家的UUID,以及它的名字历史记录。
$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' )
<?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);