Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/discord-php/discordphp-http
Asynchronous HTTP client used for communication with the Discord REST API.
https://github.com/discord-php/discordphp-http
discord discord-api
Last synced: 7 days ago
JSON representation
Asynchronous HTTP client used for communication with the Discord REST API.
- Host: GitHub
- URL: https://github.com/discord-php/discordphp-http
- Owner: discord-php
- License: mit
- Created: 2021-01-02T01:29:05.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-27T20:07:48.000Z (10 days ago)
- Last Synced: 2024-12-30T01:39:49.588Z (7 days ago)
- Topics: discord, discord-api
- Language: PHP
- Homepage:
- Size: 122 KB
- Stars: 21
- Watchers: 4
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DiscordPHP-Http
Asynchronous HTTP client used for communication with the Discord REST API.
## Requirements
- PHP >=7.4
## Installation
```sh
$ composer require discord-php/http
```A [psr/log](https://packagist.org/packages/psr/log)-compliant logging library is also required. We recommend [monolog](https://github.com/Seldaek/monolog) which will be used in examples.
## Usage
```php
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:
```php
$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:
```php
$http->queueRequest(string $method, string $url, $content, array $headers = []);
```All methods return the decoded JSON response in an object:
```php
// 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](src/Discord/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:```php
// 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](https://github.com/discord-php/DiscordPHP).## License
This software is licensed under the MIT license which can be viewed in the [LICENSE](LICENSE) file.
## Credits
- [David Cole](mailto:[email protected])
- All contributors