Download the PHP package ylorant/twitch-php-client without Composer
On this page you can find all versions of the php package ylorant/twitch-php-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ylorant/twitch-php-client
More information about ylorant/twitch-php-client
Files in ylorant/twitch-php-client
Package twitch-php-client
Short Description PHP client for Twitch's multiple APIs, easily extendable and embeddable.
License MIT
Informations about the package twitch-php-client
Twitch PHP Client
This library is a PHP client for the Twitch APIs. It will allow you to send requests to Twitch easily, using
authentication or not, with a simple design that allows embedding into services. It also relies on as few PHP
dependencies as possible, it only needs the cURL extension to be installed, and depends on psr/log
for its
LoggerInterface.
Compatibility : PHP 7.0+
Note: this library is still quite incomplete API-wise. Those will be added progressively. You can still add functionality quite easily though by adding to the already existing endpoints or by creating missing endpoints. If then you want to contribute it back to the project, just do a pull request :)
Install
Using composer:
Basic usage (using Helix)
For more info about token providers and authentication, see the next section.
Authentication: getting tokens
For some write operations, Twitch requires you to use an access token linked to the entity you want to edit. For this purpose, there's an API available to request access tokens to specific user accounts (using the "Authorize app" page you might have seen already) with multiple authentication workflows.
This library provides a way to fetch tokens using the OAuth Authorization code workflow. Here is a sample code that
would work to fetch an access token. You can see another example in the file tests/authentication.php
.
The TokenProvider interface
Twitch API relies on user and app authentication to allow access to its API (OAuth2), so to account for that,
this library uses a simple interface called the TokenProvider (at TwitchClient\Authentication\TokenProvider
).
This interface describes how the library will retrieve app and user authentication.
You can choose to implement your own token provider by creating an object implementing the TokenProvider
interface,
in case you need to store specifically your user tokens, or, if you use a more basic workflow that needs to set
the tokens once, you can use the DefaultTokenProvider
to implement a standard token provider functionality to the
client. This default provider however will only save tokens in variables, so you won't have any persistence.
The token provider works on a basic principle. You can register a set of tokens for a defined "target" channel. The targeted channel can be different from the one you create a token with. Then, when you call an api method that will require some level of access, the library will try to fetch the tokens for the targetted channel. If the token has expired, the library will try to refresh it.
When no targeted channel is needed, there is 2 cases that will happen to provide authentication :
- You can provide a default target, that will make the library use the tokens linked to that target channel by default
for all the calls where no target is defined. To use it, just call the
setDefaultTarget
method on your token provider. - You can set a default token, that isn't linked to any channel (using the client credentials flow). This won't need
any user interaction to start queries, and is the minimum required step to use the Helix API. To automatically set it,
initialize a new Authentication API client with your token provider, and call the
getClientCredentialsToken
method.
Testing
Before testing, you need to install development dependencies :
This will install phpunit, Faker and Monolog to ensure debbuging logs, fake data and the testing framework itself.
To test the app, duplicate and rename the phpunit.xml.dist
file and rename it to phpunit.xml
, then fill out in it
the info relative to the app credentials (you can use the tests/authentication.php
file to generate an access token).
Once this is done, you can start tests using the phpunit
command.