Non-blocking HTTP client and OAuth2 helper for the Twitch Helix API,
built on ReactPHP. It is the transport layer used by
TwitchPHP — the same split as
discord-php/http under DiscordPHP — and is usable on its own.
- Every call gets the
Authorization: BearerandClient-Idheaders Twitch requires. - A bounded concurrency queue, automatic
429handling (honouringRatelimit-Reset), and transient5xxretry with backoff. - Typed exceptions (
InvalidTokenException,NoPermissionsException,RateLimitException, …). Twitch\Http\OAuthcovers the client-credentials, authorization-code, refresh, and device-code flows, plusvalidate/revoke.
composer require twitchphp/httpRequires PHP 8.1+.
use React\EventLoop\Loop;
use Twitch\Http\Endpoint;
use Twitch\Http\Http;
use Twitch\Http\OAuth;
$loop = Loop::get();
$oauth = new OAuth('client-id', 'client-secret', $loop);
$oauth->clientCredentials()->then(function (array $token) use ($loop) {
$http = Http::create($token['access_token'], 'client-id', null, $loop);
// GET https://api.twitch.tv/helix/users?login=twitchdev
$http->get(
(new Endpoint(Endpoint::USERS))->addQuery('login', 'twitchdev')
)->then(function (?array $body) {
// $body === ['data' => [[...user...]], 'pagination' => []]
print_r($body['data'][0]);
});
});
$loop->run();Http resolves with the decoded JSON body untouched — Twitch's
{ "data": [...], "pagination": { "cursor": "..." } } envelope is passed
straight through, and a 204 resolves with null. Higher-level unwrapping,
Parts, repositories and events live in TwitchPHP.
MIT — see LICENSE.