Top.gg Documentation

/

PHP

Official Top.gg PHP library


PHP Library

This is our official PHP Library for Top.gg. If you have any issues, please submit an Issue on our GitHub.

Installation

composer require top-gg/php-sdk

Examples

Getting Started

This library follows the PSR standards given by PHP. Because of compliance with this, some things are done differently compared to other API wrappers. Below is sample code on how to initialize the API for use.

# Automatically load up the source library code.
include_once __DIR__ . "vendor/autoload.php";

use DBL\DBL;

$api = new DBL([
      "token" => "YOUR TOP.GG API TOKEN HERE."
]);

Below are the currently accepted list of parameters when initializing the API.

OptionTypeDescriptionOptional?
tokenstringThe token key of the Top.gg API.No
auto_statsarray[]Details for automatic stats.Yes
safetyboolWebserver code protection.Yes

The API wrapper also presents you with a boolean instance, connected which may be used to check if your API token key is still valid or not.

Get statistics from Top.gg

In order to get statistics about a bot or user from Top.gg, you are able to select between the two interchangably with the same method, find_info().

public function find_info(string $type, int $id): array
ArgumentTypeDescription
$typestringWhether you're looking for a bot or user.
$idintThe bot/user ID.
use DBL\API\Http;

if($api->connected)
{
      /**
       * You will always be presented two constants to choose.
       * 
       * Http::BOT = "bots"
       * Http::USER = "users"
       */
      $user = $api->find_info(Http::USER, 242351388137488384);
      $bot = $api->find_info(Http::BOT, 799697654279307314);
      
      print_r($user) . print_r($bot) . PHP_EOL;
}

Search for specific bots

Similar to the same premise as using find_info(), you are able to additionally plug in query information that will be automatically formatted as a query-string URL search when an HTTP request is made by the API wrapper for you. This is known as show_info().

public function show_info(string $type, array $json = []): array
ArgumentTypeDescription
$typestringWhether you're looking for a bot or user.
$jsonarrayInformation to plug into the search.
use DBL\API\Http;

if($api->connected)
{
      $small_list = $api->show_info(
            Http::BOT,
            [
                  "limit" => 20,
                  "sort" => "-"
            ]
      );
      
      print_r($small_list) . PHP_EOL;
}

Checking a vote from a user

It's very important to be able and see if a user has voted for you bot. Below is the given example of how to do this.

public function get_user_vote(int $id, int $user): array
ArgumentTypeDescription
$idintThe bot ID.
$userintThe user's snowflake ID.
if($api->connected)
{
      $isVoted = $api->get_user_vote(799697654279307314, 242351388137488384);
      if($isVoted) echo "This user voted!";
}
Edit on GitHub