This package is abandoned and no longer maintained. No replacement package was suggested.

Twitch.Tv API Wrapper in php

1.1 2016-02-25 07:48 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:55:00 UTC


Latest Stable Version Total Downloads Latest Unstable Version License StyleCI

Twitch API wrapper in PHP

  1. Installation
  2. Basic usage
  3. Authenticated requests
  4. Resources


composer require raideer/twitch-api


Basic, unauthenticated requests.

First we need to create the main Wrapper instance. The constructor takes a Guzzle Client as its only parameter.

$client = new GuzzleHttp\Client;

$wrapper = new Raideer\TwitchApi\Wrapper($client);

Now we can access various Twitch API resources using the wrapper.
You can see all the resources and their methods here:

 * Wrapper->resource->function()

$response = $wrapper->Channels->getChannel('lirik');
$response = $wrapper->resource('channels')->getChannel('lirik');

Note: letter capitalization desn't matter, so $wrapper->CHANNELS->getChannel() will still work.

Some methods can take an optional array of parameters.
See the official twitch api documentation for the list of parameters.

$wrapper->Channels->getFollows('lirik', ['limit' => 40, 'direction' => 'asc']);

Authenticated requests

First we need to create an OAuth object, that will contain the necessary information for authenticating our requests.

Read more about authentication here.

$settings = [
  'client_id'     => 'Your Client ID',
  'client_secret' => 'Your Client Secret',
  'redirect_uri'  => 'Your registered redirect URI',
  'state'         => 'Your provided unique token',
  'scope'         => 'Array or space seperated string of scopes'

$oauth = new Raideer\TwitchApi\OAuth($settings);

//Returns the authentication URL
$url = $oauth->getUrl();

Once the user authorizes your application, they will be redirected to your specified URI with a code, that's necessary for obtaining the access token.

Obtaining the Access Token

You can obtain the access token by using the getResponse method. If the request is successful, OAuthResponse object will be returned. It contains the access token, refresh token and registered scopes.

$response = $oauth->getResponse($code);

$response->getRefreshToken(); //Token refreshing isn't implemented yet

Now you can authorize the Wrapper using the authorize method.

//You can pass the OAuthResponse object directly

//Or just pass the access token

//Or pass both access token and registered scopes array
$wrapper->authorize($accessToken, $registeredScopes);

If you authorize the wrapper only by passing the access token, the Wrapper will not be able to check whether the scope you're trying to access actually exists. (Useful if you don't want to make unnecessary requests)

Now you can make authorized requests.

$wrapper->Channels->getChannel(); //Returns the authenticated user's channel

If the request is out of scope, Raideer\TwitchApi\Exceptions\OutOfScopeException will be thrown.

Throttling (under construction)

If you need, you can enable request throttling.


Time (milliseconds) between requests can be set like so:

// 1 second throttle


$client = new GuzzleHttp\Client;
$wrapper = new Raideer\TwitchApi\Wrapper($client);

$settings = [
  'client_id'     => 'myClientId',
  'client_secret' => 'myClientSecret',
  'redirect_uri'  => 'http://localhost',
  'state'         => 'myUniqueToken123123',
  'scope'         => ['channel_editor']

$oauth = new Raideer\TwitchApi\OAuth($settings);

// You can also add a scope using the addScope method

$url = $oauth->getUrl();
$code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_STRING);
$response = $oauth->getResponse($code);

$channel = $wrapper->Channels->getChannel();

echo "I'm currently playing " . $channel->game;

Resources and their methods