Examples - marcocusano/discord-php GitHub Wiki
You need user.id
and user.avatar
keys to request a User Avatar then first of all you must send a request to get a User:
$user = $discord->users->get("USER_ID");
You will receive a direct link of the Avatar inside $avatar
. A randomAvatar will be used on Avatar not found.
$avatar = $discord->medias->userAvatar($user->id, $user->avatar);
<form id="loginForm" action="https://discordapp.com/api/oauth2/authorize" method="GET">
<input type="text" class="hide hidden" name="client_id" value="<?php echo $discord->config->client_id" readonly="">
<input type="text" class="hide hidden" name="response_type" value="code" readonly="">
<input type="text" class="hide hidden" name="scope" value="identify email" id="login_scope">
<input type="text" class="hide hidden" name="redirect_uri" value="https://yourdomain.xyz/redirect">
</form>
On Form submit, you will be redirected to the Discord Authorization area that will give you back a ?code
- $_GET["code"]
param to your redirect https://yourdomain.xyz/redirect
.
$params = array(
"redirect_uri" => "https://www.yourdomain.xyz/redirect",
"client_id" => "YOUR_CLIENT_ID", // Optional (Default: $discord->config->client_id)
"client_secret" => "YOUR_CLIENT_ID", // Optional (Default: $discord->config->client_secret)
"grant_type" => "grant_type_key", // Optional (Default: authorization_code)
"scope" => "scope1 scope2 scope3 scopeN" // Optional (Default: identify)
);
$redirect = $discord->oauth2->authorize($params);
You can now use a direct link like that:
<a href="<?php echo $redirect; ?>">Login with Discord</a>
On Discord Authorization redirect you will receive back the $_GET["code"]
var that you've to exchange with a token:
$params = array(
"code" => $_GET["code"],
"redirect_uri" => "https://www.yourdomain.xyz/redirect"
"scope" => "identify email" // Optional (Default: identify) - You must specify any scope if you are using a different combination like "identify email" or "identify email guilds guilds.join".
);
$token = $discord->oauth2->getToken($params);
{
"access_token": "Y0uR_AcC3SS_T0k3N",
"token_type": "Bearer",
"expires_in": 604800,
"refresh_token": "Y0uR_R3Fr3sh_T0k3N",
"scope": "identify email"
}
Now you have a $token
that you can use to get all the informations scoped of a user:
$user = $discord->users->get(null, $token);
{
"username" : "Rivenworth",
"verified" : true,
"locale" : "it",
"premium_type" : 3,
"mfa_enabled" : false,
"id" : "197337539105914890",
"flags" : 768,
"avatar" : "a_ce6979f8afef2f484edb41d1d3f069fb",
"discriminator" : "0001",
"email" : "[email protected]"
}