19JXFGfRUV4NedS5tBGfJhkfRrN2EQtxVo
A PHP implementation to interact with the Twitch API.
-
Login here to create the application registration:
a. Give it a name b. Specify the URL to return to c. Select the category: Website Integration d. Click the Create button.
-
Click the Manage button to view the registration's details.
-
Copy the Client ID. (You’ll use to get your access token and to set the Client-Id header in all API requests. Client IDs are considered public and can be embedded in a web page’s source.)
-
Click the New Secret button then click OK on the pop up window. Copy the Client Secret. (This is passed to the token exchange endpoints to obtain a token. You must keep this confidential.)
NOTE: Do not share client IDs among applications; each application must have its own client ID. Sharing client IDs among applications may result in the suspension of your application’s access to the Twitch API.
The methods return data. To identify what it returned, you can print it to the screen with print_r().
-
Require the class and create an instance of the Twitch API integration.
require_once 'Twitch.class.php' ; $twitch = new Twitch( <Client ID>, <Client Secret> ); -
If your application needs an application token, this is how you request it:
$twitch->Get_AppToken();An application token gives you access to their public (non-sensitive) information and doesn't require the user's approval.
-
If your application needs a user approved token, this is how you request it:
First, get the user to approve your request for permissions. It will return an authorization code if they do.
// Get the User Authorization Code (without providing the optional state variable) $twitch->Request_UserAuthorization( "https://site.com/Twitch.php", <List Of Permissions>, null ); // Get the User Authorization Code (with the optional state variable) $twitch->Request_UserAuthorization( "https://site.com/Twitch.php", <List Of Permissions>, <Some State Value> );Get the User Approved Token by submitting the Authorization Code you just received in the GET response.
// Get the User Authorized Token using that code. $twitch->Get_UserToken( <Authorization Code>, "https://site.com/Twitch.php" ); -
If you need to validate the existing user approved token, this is how you do it:
$twitch->Validate_AccessToken( <User Access Token> ); -
Submit API Requests
To identify the required parameters to pass in the requests, please take a look at the Twitch API Reference.
// Get the analytics of a game played by a user $twitch->Get_TwitchData( <User/App Access Token>, "analytics/games?game_id=493057&started_at=2024-01-01T00:00:00Z&ended_at=2024-10-01T00:00:00Z" ); // Get a user's details $twitch->Get_TwitchData( <User/App Access Token>, "users?login=auronplay&login=zackrawrr" )$twitch->Delete_TwitchData( $access_token, "channel_points/custom_rewards?broadcaster_id=12345678-0dar-3fgt-de47-6431c586a7bc" )// Disable a custom reward $data = array( "broadcaster_id" => "12345678", "title" => "test", "cost" => "500000", "id" => "05582bc1-0dar-3fgt-de47-6431c586a7bc", "is_enabled" => true ); $twitch->Patch_TwitchData( $access_token, "channel_points/custom_rewards", $data );// Start a 30s commercial $data = array( "broadcaster_id" => "12345678", "length" => "30" ); $twitch->Post_TwitchData( <User/App Access Token>, "channels/comercial", $data ); // Create a custom reward $data = array( "broadcaster_id" => "12345678", "title" => "Awesome Reward", "cost" => "500000" ); $twitch->Post_TwitchData( <User/App Access Token>, "channel_points/custom_rewards", $data );// Change the user's color in chat $data = array(); $twitch->Put_TwitchData( <User/App Access Token>, "chat/color?user_id=12345678&color=blue", $data );
