Dashboard
Server ID:
No servers found.
accessToken = $accessToken; $this->baseUrl = $baseUrl; } /** * Makes a request to the Discord API. * * @param string $endpoint * @param string $method * @return mixed|null * @throws Exception */ private function apiRequest(string $endpoint, string $method = 'GET') { $url = $this->baseUrl . $endpoint; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer " . $this->accessToken, "Content-Type: application/json", // Important for some API calls ]); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // Set the method $response = curl_exec($ch); if (curl_errno($ch)) { throw new Exception('cURL error: ' . curl_error($ch)); } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode >= 400) { throw new Exception("HTTP error: " . $httpCode . " - " . $response); } $data = json_decode($response, true); return $data; } /** * Gets the user's guilds (servers). * * @return array|null * @throws Exception */ public function getUserGuilds(): ?array { return $this->apiRequest('/users/@me/guilds'); } /** * Gets the user's information. * * @return array|null * @throws Exception */ public function getUserInfo(): ?array { return $this->apiRequest('/users/@me'); } } // Instantiate the Discord API client $discordApi = new DiscordAPI($_SESSION['access_token'], $discordApiBaseUrl); try { // Fetch user's servers $guilds = $discordApi->getUserGuilds(); // Fetch user info for greeting $userInfo = $discordApi->getUserInfo(); } catch (Exception $e) { // Handle API errors gracefully echo "An error occurred: " . $e->getMessage(); exit; // Or display an error message to the user } ?>
Server ID:
No servers found.