Home » Php » PHP CURL: Discord api oAuth join guild not working

PHP CURL: Discord api oAuth join guild not working

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m working on a Discord API class (Something like discord.php, which I want to put open source after finishing it all). Here is a part of the class:

<?php
class RoDiscord {

    private $client_id;
    private $client_secret;

    private $authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
    private $tokenURL = 'https://discordapp.com/api/oauth2/token';
    private $apiURLBase = 'https://discordapp.com/api/users/@me';

    function __construct ( $client_id, $client_secret ) {
        $this -> client_id = $client_id;
        $this -> client_secret = $client_secret;
    }

    public function login ($redirect, $scope) {
        header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query(array(
            "client_id" => $this -> client_id,
            "redirect_uri" => $redirect,
            "response_type" => 'code',
            "scope" => $scope)));
        die();
    }

    public function get_token ($redirect, $code) {
        $ch = curl_init($this -> tokenURL);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
            "grant_type" => "authorization_code",
            "client_id" => $this -> client_id,
            "client_secret" => $this -> client_secret,
            "redirect_uri" => $redirect,
            "code" => $code)));
        $headers[] = 'Accept: application/json';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        return json_decode($response);
    }

    public function get_info ($access_token) {
        $ch = curl_init($this -> apiURLBase);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $headers[] = 'Accept: application/json';
        $headers[] = 'Authorization: Bearer ' . $access_token;
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        return json_decode($response);
    }

    public function join_guild ($access_token, $guildid) {
        $user = $this -> get_info ($access_token);
        $ch = curl_init("https://discordapp.com/api/guilds/".$guildid."/members/".$user -> id);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_PUT, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("access_token" => $access_token)));
        $headers[] = 'Accept: application/json';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
        return json_decode($response);
    }

}
?>

This is how I tested it:

$discord = new RoDiscord ( "CLIENT_ID", "SECRET_ID" );
if (!isset($_GET['code'])) {
    $discord -> login ( 'https://example.com/index.php', 'identify email guilds guilds.join' );
} else {
    $tokenInfo = $discord -> get_token ( 'https://example.com/index.php', $_GET['code'] );
    var_dump ( $tokenInfo );
    echo '<br />';
    var_dump ( $discord -> get_info ($tokenInfo -> access_token ) );
    echo '<br />';
    var_dump ( $discord -> join_guild ( $tokenInfo -> access_token, GUILD_ID ) );
}

The join_guild doesn’t seem to work, it returns http error 400. The Discord docs show this: https://discordapp.com/developers/docs/resources/guild#add-guild-member

Thanks for your help,

How to&Answers: