如何在discord php中通过id获取用户徽章名称?

j0pj023g  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(208)

我试图开发一个小脚本,将显示所有的徽章x不和谐用户只需输入用户id,但我不知道如何做到这一点。
这就是PHP API所显示的

{
"id": "1090372582781497424",
"username": "shaydew",
"avatar": "4bda9edf7144f97f6d4375829662b9c0",
"discriminator": "0001",
"public_flags": 4194561,
"flags": 4194561,
"banner": "a_047a91b3560dff93f74ebfdb6810578b",
"accent_color": null,
"global_name": "shay",
"avatar_decoration": "v2_a_37dc2b53b273a457ff19ac2e3fda7e4c",
"display_name": "shay",
"banner_color": null
}

“public_flags”应该获取标志的名称,有问题,我如何才能获得它?
这里我展示了我的PHP代码:

<?php
error_reporting(0);

    
    $discordId = $_GET["discord_id"];
    $token = "";
    $url = "https://discord.com/api/users/{$discordId}";
    
    $options = array(
        'http' => array(
            'header'  => "Authorization: Bot {$token}\r\n",
            'methods'  => 'GET',
        ),
    );
    
    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    if ($response === false) {
        die('Error fetching data from Discord API');
    }
    
    header('Content-Type: application/json');
    echo $response;

    
    
?>

我想让它告诉我的是:

https://discordlookup.mesavirep.xyz/v1/user/604779545018761237

"badges": [
"HOUSE_BRAVERY",
"EARLY_VERIFIED_BOT_DEVELOPER",
"ACTIVE_DEVELOPER"
],

有人能帮我得到x discord用户的徽章的名字吗?,提前非常感谢!

j2datikz

j2datikz1#

您可以在数组中转换接收到的JSON,并重新创建一个仅包含所需数据的新JSON:

$arrResponse = json_decode($response, true);
$badges = json_encode (['badges' => $arrResponse['badges']] /*, JSON_PRETTY_PRINT*/);

header('Content-Type: application/json');
echo $badges;

相关问题