Giter VIP home page Giter VIP logo

discordphp-http's Introduction

DiscordPHP-Http

Asynchronous HTTP client used for communication with the Discord REST API.

Requirements

  • PHP >=7.4

Installation

$ composer require discord-php/http

A psr/log-compliant logging library is also required. We recommend monolog which will be used in examples.

Usage

<?php

include 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Discord\Http\Http;
use Discord\Http\Drivers\React;

$loop = \React\EventLoop\Factory::create();
$logger = (new Logger('logger-name'))->pushHandler(new StreamHandler('php://output'));
$http = new Http(
    'Bot xxxx.yyyy.zzzz',
    $loop,
    $logger
);

// set up a driver - this example uses the React driver
$driver = new React($loop);
$http->setDriver($driver);

// must be the last line
$loop->run();

All request methods have the same footprint:

$http->get(string $url, $content = null, array $headers = []);
$http->post(string $url, $content = null, array $headers = []);
$http->put(string $url, $content = null, array $headers = []);
$http->patch(string $url, $content = null, array $headers = []);
$http->delete(string $url, $content = null, array $headers = []);

For other methods:

$http->queueRequest(string $method, string $url, $content, array $headers = []);

All methods return the decoded JSON response in an object:

// https://discord.com/api/v8/oauth2/applications/@me
$http->get('oauth2/applications/@me')->done(function ($response) {
    var_dump($response);
}, function ($e) {
    echo "Error: ".$e->getMessage().PHP_EOL;
});

Most Discord endpoints are provided in the Endpoint.php class as constants. Parameters start with a colon, e.g. channels/:channel_id/messages/:message_id. You can bind parameters to then with the same class:

// channels/channel_id_here/messages/message_id_here
$endpoint = Endpoint::bind(Endpoint::CHANNEL_MESSAGE, 'channel_id_here', 'message_id_here');

$http->get($endpoint)->done(...);

It is recommended that if the endpoint contains parameters you use the Endpoint::bind() function to sort requests into their correct rate limit buckets. For an example, see DiscordPHP.

License

This software is licensed under the MIT license which can be viewed in the LICENSE file.

Credits

discordphp-http's People

Contributors

sqko avatar davidcole1340 avatar exanlv avatar key2peace avatar ellisonpatterson avatar jocafamaka avatar l3aro avatar shehi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.