I am currently attempting to query the user information for any given discord account. Its pretty simple, I give the website a Discord User ID and it outputs that specific users account information.
I believe my issue is related directly to how discord authorizes their bots and after about an hour of google searching I figured it would be better to ask here.
Any help much appreciated!
Current Code:
$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);
Return From Website:
{"message": "405: Method Not Allowed", "code": 0}
If the problem is still not solved here is the solution:
<?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;
?>
If you want to be able to retrieve the data you add this for example:
$data = json_decode($response, true);
if ($data === null) {
die('Error decoding JSON data');
}
So you can use the elements by retrieving them like this:
$discordAvatar = $data['avatar'];
$discordUsername = $data['username'];
$discordDiscriminator = $data['discriminator'];
$discordId = $data['id'];