我目前正在尝试查询任何给定的不和谐帐户的用户信息。它非常简单,我给网站一个 Discord 用户 ID,它会输出特定用户的帐户信息。
我相信我的问题与不和谐如何授权他们的机器人直接相关,经过大约一个小时的谷歌搜索后,我认为最好在这里问。
非常感谢任何帮助!
当前代码:
$IDURL = "https://discord.com/api/v8/users/" . $UserID;
$CurlInitID = curl_init();
curl_setopt_array( $CurlInitID, [
CURLOPT_URL => $IDURL,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bot *private key removed*"
]
]);
$Return = curl_exec($CurlInitID);
curl_close($CurlInitID);
die($Return);
从网站返回:
{"message": "405: Method Not Allowed", "code": 0}
如果问题仍然没有解决这里是解决方案:
<?php
$discordId = "YourDiscordID";
$token = "YourToken";
$url = "https://discord.com/api/v9/users/{$discordId}";
$options = array(
'http' => array(
'header' => "Authorization: Bot {$token}\r\n",
'method' => '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;
?>
如果您希望能够检索数据,请添加以下内容:
$data = json_decode($response, true);
if ($data === null) {
die('Error decoding JSON data');
}
因此您可以通过像这样检索元素来使用它们:
$discordAvatar = $data['avatar'];
$discordUsername = $data['username'];
$discordDiscriminator = $data['discriminator'];
$discordId = $data['id'];