discord-php / http
处理对 Discord 服务器的 HTTP 请求
v10.3.2
2024-09-21 20:22 UTC
Requires
- php: ^7.4|^8.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- react/http: ^1.2
- react/promise: ^2.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.17
- guzzlehttp/guzzle: ^6.0|^7.0
- mockery/mockery: ^1.5
- monolog/monolog: ^2.2
- phpunit/phpunit: ^9.5
- psy/psysh: ^0.10.6
- react/async: ^4 || ^3
Suggests
- guzzlehttp/guzzle: For alternative to ReactPHP/Http Browser
- dev-master
- v10.3.2
- v10.3.1
- v10.3.0
- v10.2.2
- v10.2.1
- v10.2.0
- v10.1.7
- v10.1.6
- v10.1.5
- v10.1.4
- v10.1.3
- v10.1.2
- v10.1.1
- v10.1.0
- v10.0.4
- v10.0.3
- v10.0.2
- v10.0.0
- v9.x-dev
- v9.1.9
- v9.1.8
- v9.1.7
- v9.1.6
- v9.1.5
- v9.1.4
- v9.1.3
- v9.1.2
- v9.1.1
- v9.1.0
- v9.0.12
- v9.0.11
- v9.0.10
- v9.0.9
- v9.0.8
- v9.0.7
- v9.0.6
- v9.0.5
- v9.0.4
- v9.0.3
- v9.0.2
- v9.0.1
- v9.0.0
- v8.x-dev
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.0.3
- v8.0.2
- v8.0.1
- v8.0.0
- dev-promise-v2-v3-transition
- dev-key2peace-patch-1
This package is auto-updated.
Last update: 2024-09-21 20:46:46 UTC
README
用于与 Discord REST API 通信的异步 HTTP 客户端。
要求
- PHP >=7.4
安装
$ composer require discord-php/http
还需要一个符合 psr/log 协议的日志库。我们推荐使用 monolog,它将在示例中使用。
用法
<?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();
所有请求方法都有相同的结构
$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 = []);
对于其他方法
$http->queueRequest(string $method, string $url, $content, array $headers = []);
所有方法都以对象形式返回解码后的 JSON 响应
// 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; });
大多数 Discord 端点在 Endpoint.php 类中以常量形式提供。参数以冒号开头,例如 channels/:channel_id/messages/:message_id
。您可以使用相同的类将参数绑定到它们
// channels/channel_id_here/messages/message_id_here $endpoint = Endpoint::bind(Endpoint::CHANNEL_MESSAGE, 'channel_id_here', 'message_id_here'); $http->get($endpoint)->done(...);
如果端点包含参数,建议您使用 Endpoint::bind()
函数将请求排序到正确的速率限制桶中。有关示例,请参阅 DiscordPHP。
许可证
本软件根据 MIT 许可证授权,可在 LICENSE 文件中查看。
致谢
- David Cole
- 所有贡献者