2017 © Pedro Peláez
 

library twitch-api

image

obnoxiousfrog/twitch-api

  • Monday, August 21, 2017
  • by eklundchristopher
  • Repository
  • 3 Watchers
  • 6 Stars
  • 84 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Twitch Api

This is a PHP library to simplify working with the Twitch API., (*1)

PLEASE READ, (*2)

It has come to my attention that this package may no longer be compatible with the recent Twitch API changes. Unfortunately, I do not have the time required to look into it at the moment. Hopefully, I will some day in the not too distant future, and maybe I'll even go ahead and re-write it completely. I will not offer support on any major feature requests or issues until then., (*3)

Please keep this in mind if you decide to use and/or rely on this package., (*4)

Thank you., (*5)

Installation

Install by running the following Composer command:, (*6)

composer require obnoxiousfrog/twitch-api

Example

Below is an example for authenticating the user, checking if the user is following and subscribed to the user obnoxiousfrog, and if they are, they are greeted with a message. If not, an exception is thrown for logging, and/or redirecting the user to another page with an error message., (*7)

<?php require_once 'vendor/autoload.php';

try {

    $auth = new TwitchApi\Authenticator(new TwitchApi\Application(
        '3nyv5fcqdpg8uakewz6mj27r49xhbs',
        'aqdrje795sh4nm2fk8btpwuvy3cg6z',
        ['user_read', 'user_subscriptions'],
        'https://www.domain.tld/authenticate'
    ));

    if ( ! $auth->authenticate() or ! ($user = $auth->user()))
    {
        throw new Exception('Oops, something went wrong!');
    }

    if ( ! $user->isFollowing('obnoxiousfrog'))
    {
        throw new Exception('You are not a follower of ObnoxiousFrog!');
    }

    if ( ! $user->isSubscribed('obnoxiousfrog'))
    {
        throw new Exception('You are not a subscriber of ObnoxiousFrog!');
    }

    echo 'Welcome, '.$user->displayname().'!';

} catch (Exception $e) {
    // Log error
}

Documentation

TwitchApi\Application

This class implements the \TwitchApi\Contracts\Application contract., (*8)

The entire API revolves around the Application object., (*9)

$twitch = new TwitchApi\Application('client', 'secret', ['scopes'], 'redirect');

client is your Twitch application client id., (*10)

secret is your Twitch application client secret (_do not share this with anyone_)., (*11)

scopes is an array of Twitch scopes., (*12)

redirect is the Twitch application redirect url., (*13)

Below is an example of how this might look in a production environment., (*14)

$twitch = new TwitchApi\Application(
    '3nyv5fcqdpg8uakewz6mj27r49xhbs',
    'aqdrje795sh4nm2fk8btpwuvy3cg6z',
    ['user_read', 'user_subscriptions'],
    'https://www.domain.tld/authenticate',
);

Class Methods

Visibility Method Arguments Return
public __construct string $client string $secret array $scopes string $redirect void
public request - \TwitchApi\Contracts\Request
public bind string $abstract, string $concrete void
public instance string $abstract, array $parameters [] \StdClass
public make string $abstract, array $parameters [] \StdClass
private instantiate string $concrete, array $parameters \StdClass
public static api - string
public static client - string
public static secret - string
public static scopes - string
public static redirect - string

TwitchApi\Authenticator

This class implements the \TwitchApi\Contracts\Authenticator contract., (*15)

$auth = new TwitchApi\Authenticator($twitch);

Class Methods

Visibility Method Arguments Return
public __construct \TwitchApi\Contracts\Application $app void
public authenticate - boolean
public user - \TwitchApi\Contracts\User
public channel string $channel \TwitchApi\Contracts\Channel
public static url string $client null, string $redirect null, string $scopes null string

TwitchApi\User

This class implements the \TwitchApi\Contracts\User contract., (*16)

Class Methods

Visibility Method Arguments Return
public __construct \TwitchApi\Contracts\Application $app, \TwitchApi\Contracts\Response $token, \TwitchApi\Contracts\Response $user void
public accessToken - string
public refreshToken - string
public id - integer
public email - string
public name - string
public displayname - string
public logo - string
public bio - string
public timestamps - array
public isPartnered - boolean
public isSubscribed string $streamer boolean
public isFollowing string $streamer boolean

TwitchApi\Channel

This class implements the \TwitchApi\Contracts\Channel contract., (*17)

Class Methods

Visibility Method Arguments Return
public __construct \TwitchApi\Contracts\Application $app, \TwitchApi\Contracts\User $user, string $channel void
public get - \TwitchApi\Contracts\Response
public teams - array
public emoticons - array

TwitchApi\Request

This class implements the \TwitchApi\Contracts\Request contract., (*18)

Class Methods

Visibility Method Arguments Return
public __construct \TwitchApi\Contracts\Application $app void
public get string $endpoint, array $parameters [], array $headers [] \TwitchApi\Contract\Response
public post string $endpoint, array $parameters [], array $headers [] \TwitchApi\Contract\Response

TwitchApi\Response

This class implements the \TwitchApi\Contracts\Response contract., (*19)

Class Methods

Visibility Method Arguments Return
public __construct \TwitchApi\Contracts\Application $app, string $response, array $request void
public __get string $property mixed

Using your own classes

Make sure your own classes implement the appropriate contract (_you may find all the contracts below the title of each class documentation above_), and then you may simply run the bind method from the \TwitchApi\Application object., (*20)

For example, the following would replace the TwitchApi User class with your own:, (*21)

$twitch->bind('TwitchApi\Contracts\User', 'CustomUserClass');

The Versions

21/08 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

01/06 2015

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Development Requires