PHPackages                             romanzipp/laravel-twitch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. romanzipp/laravel-twitch

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

romanzipp/laravel-twitch
========================

Twitch PHP Wrapper for Laravel

4.9.1(4mo ago)115128.2k↓48.8%20[2 issues](https://github.com/romanzipp/Laravel-Twitch/issues)4MITPHPPHP ^8.0CI failing

Since Feb 6Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/romanzipp/Laravel-Twitch)[ Packagist](https://packagist.org/packages/romanzipp/laravel-twitch)[ GitHub Sponsors](https://github.com/romanzipp)[ RSS](/packages/romanzipp-laravel-twitch/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (11)Versions (87)Used By (4)

Laravel Twitch
==============

[](#laravel-twitch)

[![Latest Stable Version](https://camo.githubusercontent.com/0089ec267b80ceb6422b6e4a80e9b44ddb1e73dede3f36b221658b252cf6a251/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f6d616e7a6970702f6c61726176656c2d7477697463682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/laravel-twitch)[![Total Downloads](https://camo.githubusercontent.com/074f90696adcdac36dcb7855ce13a803f3bdad611c142463688d947459a80fa1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f6d616e7a6970702f6c61726176656c2d7477697463682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/laravel-twitch)[![License](https://camo.githubusercontent.com/45d6ca2ec08ed47f72d06c22ffb7bb22eb817eb2560f1eb9cfe082b8d86f2229/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f726f6d616e7a6970702f6c61726176656c2d7477697463682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/laravel-twitch)[![GitHub Build Status](https://camo.githubusercontent.com/148f57a5a7e72d84379ea98d86732418dd094b32def83caab04120eb4a8ac26e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f6d616e7a6970702f4c61726176656c2d5477697463682f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/romanzipp/Laravel-Twitch/actions)

PHP Twitch Helix API Wrapper for Laravel 5+

⚠️ Changes on May 01, 2020
--------------------------

[](#️-changes-on-may-01-2020)

Since May 01, 2020, Twitch requires all requests to contain a valid OAuth Access Token. This can be achieved by requesting an OAuth Token using the [Client Credentials Flow](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-client-credentials-flow).

If you don't handle this by yourself, be sure to enable the built-in Access Token generation feature via the `oauth_client_credentials.auto_generate` configuration entry.

You will need define a valid **Client ID** and **Client Secret** via your config or the available setters! See the [full config](https://github.com/romanzipp/Laravel-Twitch/blob/master/config/twitch-api.php) for more details.

Table of contents
-----------------

[](#table-of-contents)

1. [Installation](#installation)
2. [Configuration](#configuration)
3. [Examples](#examples)
4. [Documentation](#documentation)
5. [Upgrading](#upgrading)
6. [Development](#Development)

Installation
------------

[](#installation)

```
composer require romanzipp/laravel-twitch

```

Configuration
-------------

[](#configuration)

Copy configuration to project:

```
php artisan vendor:publish --provider="romanzipp\Twitch\Providers\TwitchServiceProvider"

```

Add environmental variables to your `.env`:

```
TWITCH_HELIX_KEY=
TWITCH_HELIX_SECRET=
TWITCH_HELIX_REDIRECT_URI=http://localhost

```

If you want to use the EventSub with the Webhook transport, then you are required to define a secret. This secret is a string between 10 and 100 characters.

```
TWITCH_HELIX_EVENTSUB_SECRET=

```

Examples
--------

[](#examples)

### Basic

[](#basic)

```
$twitch = new romanzipp\Twitch\Twitch;

$twitch->setClientId('abc123');

// Get User by Username
$result = $twitch->getUsers(['login' => 'herrausragend']);

// Check, if the query was successful
if ( ! $result->success()) {
    return null;
}

// Shift result to get single user data
$user = $result->shift();

return $user->id;
```

### Setters

[](#setters)

```
$twitch = new romanzipp\Twitch\Twitch;

$twitch->setClientId('abc123');
$twitch->setClientSecret('abc456');
$twitch->setToken('abcdef123456');

$twitch = $twitch->withClientId('abc123');
$twitch = $twitch->withClientSecret('abc123');
$twitch = $twitch->withToken('abcdef123456');
```

### OAuth Tokens

[](#oauth-tokens)

```
$twitch = new romanzipp\Twitch\Twitch;

$twitch->setClientId('abc123');
$twitch->setToken('abcdef123456');

$result = $twitch->getUsers(['login' => 'herrausragend']);
```

### OAuth Client Credentials Flow

[](#oauth-client-credentials-flow)

Since May 01, 2020, every request requires an OAuth token which can be issued using the [OAuth Client Credentials Flow](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-client-credentials-flow).

```
use romanzipp\Twitch\Enums\GrantType;
use romanzipp\Twitch\Twitch;

$twitch = new Twitch;

$twitch->setClientId('abc123');
$twitch->setClientSecret('def123');
$twitch->setToken('abcdef123456');

$result = $twitch->getOAuthToken(null, GrantType::CLIENT_CREDENTIALS, ['user:read']);

if ( ! $result->success()) {
    return;
}

$accessToken = $result->data()->access_token;
```

### Pagination

[](#pagination)

The Twitch API returns a `paginator` field with paginated results like `/streams`, `/follows`or `/games`. To jump between pages, the given cursor must be appended to the following query using the direction attributes `after` or `before`.

In this example, we will fetch a set of streams and use the provided cursor to switch to the next/previous set of data.

❗️ To prevent infinite loops or errors, use the `Result::hasMoreResults()` method to check if there are more results available.

```
$twitch = new romanzipp\Twitch\Twitch;

// Page 1
$firstResult = $twitch->getStreams(['language' => 'de']);

// Page 2
$secondResult = $twitch->getStreams(['language' => 'de'], $firstResult->next());

// Page 1 (again)
$thirdResult = $twitch->getStreams(['language' => 'de'], $secondResult->back());
```

### Facade

[](#facade)

```
use romanzipp\Twitch\Facades\Twitch;

Twitch::withClientId('abc123')->withToken('abcdef123456')->getUsers();
```

### Pagination Loop Example

[](#pagination-loop-example)

This example fetches all Twitch Games and stores them into a database.

```
use romanzipp\Twitch\Twitch;

$twitch = new Twitch;

do {
    $nextCursor = null;

    // If this is not the first iteration, get the page cursor to the next set of results
    if (isset($result)) {
        $nextCursor = $result->next();
    }

    // Query the API with an optional cursor to the next results page
    $result = $twitch->getTopGames(['first' => 100], $nextCursor);

    foreach ($result->data() as $item) {
        // Process the games result data
    }

    // Continue until there are no results left
} while ($result->hasMoreResults());
```

### Insert user objects

[](#insert-user-objects)

The new API does not include the user objects in endpoints like followers or bits.

```
[
  {
    "from_id": "123456",
    "to_id": "654321",
    "followed_at": "2018-01-01 12:00:00"
  }
]
```

You can just call the [insertUsers](https://github.com/romanzipp/Laravel-Twitch/blob/master/src/Result.php#L233) method to insert all user data identified by `from_id` into `from_user`

```
use romanzipp\Twitch\Twitch;

$twitch = new Twitch;

$result = $twitch->getUsersFollows(['to_id' => 654321]);

$result->insertUsers($twitch, 'from_id', 'from_user');
```

**New Result data:**

```
[
  {
    "from_id": "123456",
    "to_id": "654321",
    "followed_at": "2018-01-01 12:00:00",
    "from_user": {
      "id": "123456",
      "display_name": "HerrAusragend",
      "login": "herrausragend"
    }
  }
]
```

### Defining EventSub Handlers

[](#defining-eventsub-handlers)

By default, the EventSub webhook controller will automatically handle all EventSub notification and revocation calls; however, if you have additional webhook events you would like to handle, you may do so by extending the EventSub webhook controller.

To ensure your application can handle EventSub webhooks, be sure to configure the webhook callback url in the transport payload.

Your controller's method names should correspond to Laravel Twitch's controller conventions. Specifically, methods should be prefixed with `handle`, suffixed with `Notification` and the "camel case" name of the EventSub Type you wish to handle. For example, if you wish to handle the `channel.follow` type, you should add a `handleChannelFollowNotification` method to the controller:

```
