PHP code example of ylorant / twitch-php-client

1. Go to this page and download the library: Download ylorant/twitch-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ylorant / twitch-php-client example snippets



use TwitchClient\API\Kraken\Kraken;
use TwitchClient\Authentication\DefaultTokenProvider;

// Create a default token provider
$tokenProvider = new DefaultTokenProvider('client_id', 'client_secret');

// Create the client for the specific API we want to query, here it's Kraken
$helix = new Helix($tokenProvider);

// Fetch info for an user, for example
$userInfo = $helix->users->getUser('esamarathon');


use TwitchClient\API\Auth\Authentication;
use TwitchClient\Authentication\DefaultTokenProvider;

// Create the token provider using the client ID and secret.
$tokenProvider = new DefaultTokenProvider('client_id', 'client_secret');
$redirectURI = 'http://localhost/'; // The redirect URI configured in the app settings on Twitch.
                                    // Here we'll suppose that we're on a single page that handles both.

$authAPI = new Authentication($tokenProvider);

// If the call has a GET parameter named 'code', then we're on the redirect URI
if (!empty($_GET['code'])) {
    // Getting the access token, the API requiring to send 
    $accessToken = $authAPI->getAccessToken($_GET['code'], $redirectURI);

    var_dump($accessToken); // Dumping it for example
} else {
    // Get an authorize URL with some scope (here the one to allow the app to change the stream title and game)
    $authorizeURL = $authAPI->getAuthorizeURL($redirectURI, ['channel_editor']);
    
    // Redirect the user to the authorize page
    header('Location: '. $authorizeURL);
}


composer install ylorant/twitch-php-client