simply-stream / twitch-api
Requires
- php: ^8.2
- ext-json: *
- cuyz/valinor: ^1.7
- league/oauth2-client: ^2.7
- psr/http-client: ^1.0
- psr/log: ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.38
- guzzlehttp/guzzle: ^7.8
- nyholm/psr7: ^1.8
- php-http/discovery: ^1.17
- phpstan/phpstan: ^1.10
- phpunit/phpunit: 10.4.2
Suggests
- guzzlehttp/guzzle: ^7.8
- nyholm/psr7: ^1.8
README
Welcome to the PHP Twitch Helix API Library, a powerful and developer-friendly implementation of the new Twitch API " Helix," complete with robust EventSub functionality. This library seamlessly integrates the latest features from Twitch, providing a straightforward and efficient way to interact with the Twitch platform in your PHP projects.
Key Features
Helix API Support: Harness the full potential of the Twitch Helix API with ease. Retrieve user information, access streams, and more, all through a clean and intuitive PHP interface.
EventSub Functionality: Embrace the future of Twitch event handling with our comprehensive EventSub implementation. Keep your application in sync with real-time events, ensuring timely and accurate updates.
Webhook Integration: Our library fully supports webhook communication for EventSub, enabling seamless communication between Twitch and your application. Stay informed about user activities and channel events effortlessly.
Data Transfer Objects (DTOs): Differentiating itself from traditional approaches, our library employs Data Transfer Objects (DTOs) to map incoming JSON responses to PHP objects. This abstraction simplifies the handling of Twitch data, enhancing code readability and maintainability.
To see a full list of implemented APIs, have a look at the Implemented APIs section.
Installation
composer req simplystream/twitch-api
Implemented APIs
❗ = Tests can't be implemented due to lack of mock-api-data. Mapping should work on Twitch prod systems ️️ℹ️ = Some tests are available, some are missing due to lack of mock-api data. Mapping should work on Twitch prod systems
Tested in this case means, that functional or unit tests exist.
There's also a container api service that can hold all the APIs implemented. See TwitchApi.
EventSub
Besides the APIs, there's also a service available for the EventSub handling. This service will handle the registration to an event and also the webhook callbacks by validating the challenge send by Twitch.
Please note, that this package only supports the webhook implementation! This is due to the fact, that PHP might not be the ideal programming language to use for long running processes like a websocket.
Websocket
To use the websocket implementation, you should check out the following projects:
- TwitchLib (C#)
- Twurple (TypeScript) and docs
- More will follow
Usage
To get everything up and running, you need to set some things up.
$client = new Client(); // Same for the request factory, it just needs to implement the RequestFactoryInterface&StreamFactoryInterface. // Optionally the UriFactoryInterface, too, if you want to use the same object for the UriFactory. $requestFactory = new RequestFactory(); $apiClient = new ApiClient( $client, $requestFactory, new \CuyZ\Valinor\MapperBuilder(), $requestFactory, ['clientId' => 'YOUR_CLIENT_ID', 'webhook' => ['secret' => 'YOUR_SECRET']] ); $usersApi = new UsersApi($apiClient) $response = $usersApi->getUsers(logins: ['some_login_name'], accessToken: $accessToken); foreach($response->getData() as $user) { echo $user->getDisplayName(); }
Bring your own client
Instead of forcing you to implement yet another HTTP client, this library gives you the opportunity to use your own.
The only restriction you got: It has to be PSR-18 compliant and implement the
interface \Psr\Http\Client\ClientInterface
.
We recommend either using the all in one package Guzzlehttp or PHP-HTTP with the PSR7 implementation Nyholm/PSR7 or Guzzle/PSR7.
AccessToken
In older versions, the AccessToken have been autogenerated by the sendRequest method by a TwitchProvider that has been
build into this library. This provider is now removed and you need to give the $api->sendRequest(...)
method an
AccessToken yourself!
Due to the fact that this library still requires the PHP league implementation of an AccessTokenInterface, we recommend using https://github.com/vertisan/oauth2-twitch-helix to generate an AccessToken.
Supported Frameworks
Currently, there is only an integration for Symfony.
- simplystream/twitch-api-bundle (Still WIP, most of the code there has been moved to this repository)
TODO List
Even though most of this library is ready to use, there's still a lot to do. Here is a brief overview of what will come next ordered more or less by priority:
- Factories/Builder to easily instantiate the APIs and maybe some DTOs, especially the TwitchApi class
- Middleware features, to easily extend requests (e.g.: RateLimitMiddleware)
- A guideline for contributions
Contribution
We welcome contributions! Feel free to open issues, submit pull requests, or join our community discussions. A short guide for contribution will follow.
Support
You really like this project and want to support us in a different way than contribution? Feel free to support me on Ko-fi ♥️