2017 © Pedro Peláez
 

library twitch-api

Twitch.Tv API Wrapper in php

image

raideer/twitch-api

Twitch.Tv API Wrapper in php

  • Tuesday, March 29, 2016
  • by Raideer
  • Repository
  • 2 Watchers
  • 3 Stars
  • 209 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Latest Stable Version Total Downloads Latest Unstable Version License StyleCI, (*1)

Twitch API wrapper in PHP

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

Installation

composer require raideer/twitch-api, (*2)

Usage

Basic, unauthenticated requests.

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

$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:
https://github.com/raideer/twitch-api/blob/master/resources.md
https://github.com/justintv/Twitch-API#index, (*4)

/*
 * Wrapper->resource->function()
 */

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

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

Some methods can take an optional array of parameters.
See the official twitch api documentation for the list of parameters., (*6)

$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., (*7)

Read more about authentication here., (*8)

$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., (*9)

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., (*10)

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

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

Now you can authorize the Wrapper using the authorize method., (*11)

//You can pass the OAuthResponse object directly
$wrapper->authorize($response);

//Or just pass the access token
$wrapper->authorize($accessToken);

//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), (*12)

Now you can make authorized requests., (*13)

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

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

Throttling (under construction)

If you need, you can enable request throttling., (*15)

$wrapper->enableThrottle(true);

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

// 1 second throttle
$wrapper->setThrottleInterval(1000);

Examples


$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 $oauth->addScope('channel_read'); $url = $oauth->getUrl();

$code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_STRING); $response = $oauth->getResponse($code); $wrapper->authorize($response); $channel = $wrapper->Channels->getChannel(); echo "I'm currently playing " . $channel->game;

Resources and their methods

The Versions

29/03 2016

dev-master

9999999-dev

Twitch.Tv API Wrapper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Raideer

25/02 2016

1.1

1.1.0.0

Twitch.Tv API Wrapper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Raideer

13/02 2016

1.0.2

1.0.2.0

Twitch.Tv API Wrapper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Raideer

13/02 2016

1.0.1

1.0.1.0

Twitch.Tv API Wrapper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Raideer

26/01 2016

1.0

1.0.0.0

Twitch.Tv API Wrapper in php

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Raideer