diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md index 85ffcd6..a7721cf 100644 --- a/README.md +++ b/README.md @@ -33,31 +33,52 @@ This class will automatically refresh access token once it expires! * Get users clips * Get game name for game id * Get game artwork for game id +* PHP 8 ## Usage -Add your Twitch client id, client secret and authorization code into the constants (lines 5-7) +**Fetch with composer:** -Add your redirect URI [info](https://write.corbpie.com/twitch-api-authentication-with-oauth-using-php/) (line 8) +```shell +composer require corbpie/twitch-api-class +``` -Change the token filename constant, however keep it as a .txt extension (line 9) +To use: -Make sure the class file is included ```php -require_once('twitch-class.php'); +require_once('vendor/autoload.php'); + +use Corbpie\TwitchApiClass\twitchWrapper; + +$call = new twitchWrapper(); ``` -Then assign a new instance +Add your Twitch client id and client secret into the constants (lines 7-8) + +Add your redirect URI [info](https://write.corbpie.com/twitch-api-authentication-with-oauth-using-php/) (line 10) + +Change the token filename constant, however keep it as a .txt extension (line 11) + +Access code can be obtained with + ```php -$call = new twitch_call(); +echo $call->accessCodeUrl(); ``` +Upon going to the link You will find the access code in the URL: + +```http://localhost/?code=THEISACCESSCODE``` + +### Calls + Get current top (view count) streams `array` + ```php $call->getTopStreams(); ``` Protect your calls against an expired access token: + ```php $data = $call->getUserLiveData('summit1g'); if (!$call->checkCallSuccess($data)) {//Call failed but refreshed token, try it again: @@ -67,17 +88,20 @@ echo $data; ``` Get current top (view count) streams for a game `array` + ```php $call->getGameTopStreams($gameid); ``` Get top (view count) streamer for a game `string` + ```php $call->getGameTopStreams($gameid); echo $call->getTopStreamerForGame(); ``` Get viewer count for the top stream for a game `string` + ```php $call->getGameTopStreams($gameid); echo $call->getTopViewersForGame(); @@ -86,141 +110,153 @@ echo $call->getTopViewersForGame(); Get top games `array` (Good way to get gameid's) + ```php $call->getTopGames(); ``` Get details for username `array` + ```php $call->getUserDetails($username); ``` Get user id for username `string` + ```php $call->getUserDetails($username); $user_id = $call->idForUser(); ``` Get emotes for username `array` + ```php $call->getUserEmotes($username); ``` Get image for emote id `string` + ```php $call->emoteImage($emoteid); ``` - Get chat for VOD `array` + ```php $call->chatForVod($vod_id, $offset); ``` - Get users stream details (If live) `array` + ```php $call->getUserStream($username); ``` Check if a user is live and streaming `boolean` + ```php $call->getUserStream($username); $call->userIsLive();//true for live | false for not live ``` + __If user is streaming:__ Get game id `string` + ```php $call->streamGameId(); ``` Get viewer count `string` + ```php $call->streamViewers(); ``` Get stream title `string` + ```php $call->streamTitle(); ``` Get stream id `string` + ```php $call->streamId(); ``` Get stream start time `string` + ```php $call->streamStart(); ``` Get stream thumbnail `string` + ```php $call->streamThumbnail(); ``` Get stream thumbnail `array` + ```php $call->getStreamTags($streamid); ``` Get top clips for game id `array` + ```php $call->getGameClips($gameid); ``` Get users top clips `array` + ```php $call->getUserClips($user); ``` Get users videos (most recent first) `array` + ```php $call->getUserVideos($user); ``` Get users videos for game id `array` + ```php $call->getUserVideosForGame($user, $game_id); ``` Get game data for game id `array` + ```php $call->getGameData($game_id); ``` Get game name `string` + ```php $call->getGameData($game_id); $game_name = $call->gameName(); ``` Get game artwork `string` + ```php $call->getGameData($game_id); $game_name = $call->gameArtwork(); ``` Get game artwork `string` + ```php $call->getGameData($game_id); $game_name = $call->gameArtwork(); ``` -Set JSON header -```php -$call->setJsonHeader(); -``` - Custom array access `string` + ```php //array return call here Eg:$call->getUserDetails('shroud'); $custom = $call->getCustom(0, 'type'); -``` - - -## TODO - -* Access to all values, not just popular ones. -* Greater options to calls, add filter types (Newest, view count, length). +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3f3abae --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "corbpie/twitch-api-class", + "description": "A PHP Twitch API wrapper", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Corbpie\\TwitchApiClass\\": "src/" + } + }, + "authors": [ + { + "name": "corbpie", + "email": "contact@corbpie.com" + } + ], + "minimum-stability": "dev", + "require": { + "php": ">=8.0", + "ext-curl": "*" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..cba2f02 --- /dev/null +++ b/composer.lock @@ -0,0 +1,20 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "8f9aec641d01074b607c89989f01cc5c", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=8.0" + }, + "platform-dev": [], + "plugin-api-version": "2.1.0" +} diff --git a/example.php b/example.php index c9ea417..52e4cbb 100644 --- a/example.php +++ b/example.php @@ -1,9 +1,10 @@ setApiKey('TWITCHCLIENTID');//Set client id -$call->getUserStream('shroud');//Set streamers username -if ($call->userIsLive()) {//Will return true is user is live/streaming - echo "Shroud is streaming right now"; -} \ No newline at end of file +require_once('vendor/autoload.php'); + +use Corbpie\TwitchApiClass\twitchWrapper; + +$call = new twitchWrapper(); + +echo $call->accessCodeUrl(); + +echo json_encode($call->getUserDetails('shroud')); \ No newline at end of file diff --git a/src/twitchWrapper.php b/src/twitchWrapper.php new file mode 100644 index 0000000..c80bdfa --- /dev/null +++ b/src/twitchWrapper.php @@ -0,0 +1,302 @@ +getTokensFromFile(); + } + + public function accessCodeUrl(string $scope = "user:edit+user:read:email"): string + { + return self::TWITCH_OUATH_URI . "/authorize?response_type=code&client_id=" . self::CLIENT_ID . "&redirect_uri=" . self::REDIRECT_URI . "&scope=$scope"; + } + + public function doCurl(string $url, string $type = 'GET', array $headers = [], array $post_fields = [], int $con_timeout = 5, int $timeout = 20): array + { + $crl = curl_init($url); + if ($type === 'POST') { + curl_setopt($crl, CURLOPT_POST, true); + if (!empty($post_fields)) { + curl_setopt($crl, CURLOPT_POSTFIELDS, $post_fields); + } + } + if (!empty($headers)) { + curl_setopt($crl, CURLOPT_HTTPHEADER, $headers); + } + curl_setopt($crl, CURLOPT_CUSTOMREQUEST, $type); + curl_setopt($crl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $con_timeout); + curl_setopt($crl, CURLOPT_TIMEOUT, $timeout); + curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, 2); + curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($crl, CURLOPT_ENCODING, 'gzip,deflate'); + curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); + $call_response = curl_exec($crl); + $responseInfo = curl_getinfo($crl); + curl_close($crl); + if ($responseInfo['http_code'] === 200) { + return json_decode($call_response, true); + } + return array('http_response_code' => $responseInfo['http_code']);//Call failed + } + + public function dateTimePassed(string $dt_to_check): bool + {//False on passed + date_default_timezone_set('UTC'); + $dt1 = strtotime(date('Y-m-d H:i:s', strtotime($dt_to_check))); + $dt2 = strtotime(date('Y-m-d H:i:s')); + return $dt1 < $dt2; + } + + private function addSecondsToDateTime(int $seconds): string + { + date_default_timezone_set('UTC'); + return date("Y-m-d H:i:s", strtotime("+{$seconds} sec")); + } + + public function getTokensFromFile(): void + { + if (!file_exists(self::TOKEN_FILENAME)) {//First time use = create the file + $this->createTokenFile(); + } + $tokens = json_decode(file_get_contents(self::TOKEN_FILENAME), false); + $this->access_token = $tokens->access_token; + $this->refresh_token = $tokens->refresh_token; + $this->expires_dt = $tokens->expires; + if ($this->dateTimePassed($this->expires_dt)) { + $this->refreshToken(); + $this->getTokensFromFile(); + } + } + + public function createTokenFile(): bool + { + $url = self::TWITCH_OUATH_URI . "/token?client_id=" . self::CLIENT_ID . "&client_secret=" . self::CLIENT_SECRET . "&code=" . self::ACCESS_CODE . "&grant_type=authorization_code&redirect_uri=" . self::REDIRECT_URI; + $data = $this->doCurl($url, "POST"); + $contents = '{"access_token": "' . $data['access_token'] . '", "refresh_token": "' . $data['refresh_token'] . '", "expires": "' . $this->addSecondsToDateTime($data['expires_in']) . '"}'; + $fp = fopen(self::TOKEN_FILENAME, 'wb'); + fwrite($fp, $contents); + return fclose($fp); + } + + public function refreshToken(): bool + { + $url = self::TWITCH_OUATH_URI . "/token?grant_type=refresh_token&refresh_token=$this->refresh_token&client_id=" . self::CLIENT_ID . "&client_secret=" . self::CLIENT_SECRET; + $data = $this->doCurl($url, 'POST'); + $contents = '{"access_token": "' . $data['access_token'] . '", "refresh_token": "' . $data['refresh_token'] . '"}'; + $fp = fopen(self::TOKEN_FILENAME, 'wb'); + fwrite($fp, $contents); + return fclose($fp); + } + + private function GETHeaders(): array + { + return array("Authorization: Bearer $this->access_token", "Client-ID: " . self::CLIENT_ID); + } + + public function getTopStreams(string $pagination = '', int $amount = 25): array + { + if ($pagination === '') { + return $this->call_data = $this->doCurl(self::URI . '/helix/streams?first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->call_data = $this->doCurl(self::URI . '/helix/streams?first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination), 'GET', $this->GETHeaders()); + } + + public function getGameTopStreams(int $game_id, string $pagination = '', int $amount = 25): array + { + if ($pagination === '') { + return $this->call_data = $this->doCurl(self::URI . '/helix/streams?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->call_data = $this->doCurl(self::URI . '/helix/streams?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination), 'GET', $this->GETHeaders()); + } + + public function getTopStreamerForGame(): string + { + return $this->call_data['data'][0]['user_name']; + } + + public function getTopViewersForGame(): string + { + return $this->call_data['data'][0]['viewer_count']; + } + + public function getTopGames(string $pagination = '', int $amount = 25): array + { + if ($pagination === '') { + return $this->call_data = $this->doCurl(self::URI . '/helix/games/top?first=' . rawurlencode($amount) . '', 'GET', $this->GETHeaders()); + } + return $this->call_data = $this->doCurl(self::URI . '/helix/games/top?first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination), 'GET', $this->GETHeaders()); + } + + public function getUserDetails(string $username): array + { + return $this->doCurl(self::URI . '/helix/users?login=' . rawurlencode($username), 'GET', $this->GETHeaders()); + } + + public function idForUser(): ?int + { + return $this->call_data['data'][0]['id'] ?? null; + } + + public function userViewCount(): ?int + { + return $this->call_data['data'][0]['view_count'] ?? null; + } + + public function getUserEmotes(string $username): array + { + return $this->doCurl(self::URI . '/api/channels/' . rawurlencode($username) . '/product', 'GET', $this->GETHeaders()); + } + + public function emoteImage(string $emote_id, string $size = '2.0'): string + { + return "https://static-cdn.jtvnw.net/emoticons/v1/$emote_id/$size"; + } + + public function chatForVod(string $vod_id, string $offset): array + { + return $this->doCurl(self::URI . '/v5/videos/' . rawurlencode($vod_id) . '/comments?content_offset_seconds=' . rawurlencode($offset), 'GET', $this->GETHeaders()); + } + + public function getUserStream(string $username): array + { + return $this->stream_data = $this->doCurl(self::URI . '/helix/streams?user_login=' . rawurlencode($username), 'GET', $this->GETHeaders()); + } + + public function userIsLive(): ?bool + { + if (isset($this->stream_data['data'][0])) { + return true;//Yes + } + return false;//No + } + + public function streamGameId(): ?string + { + return $this->stream_data['data'][0]['game_id'] ?? null; + } + + public function streamViewers(): int + { + return $this->stream_data['data'][0]['viewer_count'] ?? 0; + } + + public function streamTitle(): ?string + { + return $this->stream_data['data'][0]['title'] ?? null; + } + + public function streamId(): ?int + { + return $this->stream_data['data'][0]['id'] ?? null; + } + + public function streamUserId(): ?int + { + return $this->stream_data['data'][0]['user_id'] ?? null; + } + + public function streamStart(): ?string + { + return $this->stream_data['data'][0]['started_at'] ?? null; + } + + public function streamLanguage(): ?string + { + return $this->stream_data['data'][0]['language'] ?? null; + } + + public function streamThumbnail(): ?string + { + return $this->stream_data['data'][0]['thumbnail_url'] ?? null; + } + + public function getStreamTags(string $stream_id): array + { + return $this->doCurl(self::URI . '/helix/streams/tags?broadcaster_id=' . rawurlencode($stream_id), 'GET', $this->GETHeaders()); + } + + public function getAllStreamTags(string $tag_id): array + { + return $this->doCurl(self::URI . '/helix/tags/streams?tag_id=' . rawurlencode($tag_id), 'GET', $this->GETHeaders()); + } + + public function getGameClips(int $game_id, string $pagination = '', int $amount = 25): array + { + if ($pagination === '') { + return $this->doCurl(self::URI . '/helix/clips?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->doCurl(self::URI . '/helix/clips?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination), 'GET', $this->GETHeaders()); + } + + public function getUserGameClips(string $user_id, int $game_id, string $pagination = '', int $amount = 25): array + { + if ($pagination === '') { + return $this->doCurl(self::URI . '/helix/clips?broadcaster_id=' . rawurlencode($user_id) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->doCurl(self::URI . '/helix/clips?broadcaster_id=' . rawurlencode($user_id) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination), 'GET', $this->GETHeaders()); + } + + public function getUserClips(string $user, int $amount = 25): array + { + return $this->doCurl(self::URI . '/helix/clips?broadcaster_id=' . rawurlencode($user) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + + public function getUserVideos(string $user, string $sort_by, int $amount = 25): array + { + if ($sort_by === 'TIME' || $sort_by === 'TRENDING' || $sort_by === 'VIEWS') { + return $this->doCurl(self::URI . '/helix/videos?user_id=' . rawurlencode($user) . '&sort=' . rawurlencode($sort_by) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->doCurl(self::URI . '/helix/videos?user_id=' . rawurlencode($user) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + + public function getUserVideosForGame(string $user, int $game_id, string $sort_by, int $amount = 25): array + { + if ($sort_by === 'TIME' || $sort_by === 'TRENDING' || $sort_by === 'VIEWS') { + return $this->doCurl(self::URI . '/helix/videos?user_id=' . rawurlencode($user) . '&game_id=' . rawurlencode($game_id) . '&sort=' . rawurlencode($sort_by) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + return $this->doCurl(self::URI . '/helix/videos?user_id=' . rawurlencode($user) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount), 'GET', $this->GETHeaders()); + } + + public function getGameData(int $game_id): array + { + return $this->call_data = $this->doCurl(self::URI . '/helix/games?id=' . rawurlencode($game_id), 'GET', $this->GETHeaders()); + } + + public function gameName(): ?string + { + return $this->call_data['data'][0]['name'] ?? null; + } + + public function gameArtwork(): ?string + { + return $this->call_data['data'][0]['box_art_url'] ?? null; + } + + public function getCustom(int $level, string $key): string|array|null + { + return $this->call_data['data'][$level][$key] ?? null; + } + + private function minutesFormat(int $minutes, string $format = 'H:i:s'): string + { + return gmdate($format, $minutes); + } +} \ No newline at end of file diff --git a/twitch-class.php b/twitch-class.php deleted file mode 100644 index 385e4ec..0000000 --- a/twitch-class.php +++ /dev/null @@ -1,335 +0,0 @@ -getTokens(); - } - - public function getTokens(): void - { - $filename = self::TOKEN_FILENAME; - if (!file_exists($filename)) {//First time use = create the file - $this->createTokenFile(); - } - $tokens = json_decode(file_get_contents($filename)); - $this->access_token = $tokens->access_token; - $this->refresh_token = $tokens->refresh_token; - } - - public function createTokenFile(): void - { - $data = json_decode($this->doCurl("" . self::TWITCH_OUATH_URI . "/token?client_id=" . self::CLIENT_ID . "&client_secret=" . self::CLIENT_SECRET . "&code=" . self::ACCESS_CODE . "&grant_type=authorization_code&redirect_uri=" . self::REDIRECT_URI . "", "POST")); - $contents = '{"access_token": "' . $data->access_token . '", "refresh_token": "' . $data->refresh_token . '"}'; - $fp = fopen(self::TOKEN_FILENAME, 'wb'); - fwrite($fp, $contents); - fclose($fp); - } - - public function refreshToken(): void - { - $data = json_decode($this->doCurl("" . self::TWITCH_OUATH_URI . "/token?grant_type=refresh_token&refresh_token=$this->refresh_token&client_id=" . self::CLIENT_ID . "&client_secret=" . self::CLIENT_SECRET . "", 'POST')); - $contents = '{"access_token": "' . $data->access_token . '", "refresh_token": "' . $data->refresh_token . '"}'; - $fp = fopen(self::TOKEN_FILENAME, 'w'); - fwrite($fp, $contents); - fclose($fp); - } - - public function checkCallSuccess(): bool|string - { - if (isset($this->data['http_response_code']) && $this->data['http_response_code'] == 401) { - $this->refreshToken();//Fetches new access and refresh tokens - return false; - } else { - return json_encode($this->data); - } - } - - public function apiCall(string $url, array $params = array(), string $method = 'get'): bool|array|string - { - $url = (self::URI . $url); - $curl = curl_init(); - curl_setopt($curl, CURLOPT_TIMEOUT, 5); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_HTTPHEADER, array( - "Authorization: Bearer $this->access_token", - "Client-ID: " . self::CLIENT_ID . "" - )); - if ($method === 'get' && !empty($params)) { - $url .= '?' . http_build_query($params); - } else if ($method === 'post') { - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params)); - } - curl_setopt($curl, CURLOPT_URL, $url); - $response = curl_exec($curl); - $responseInfo = curl_getinfo($curl); - if ($responseInfo['http_code'] === 200) { - $data = $response; - } else { - $data = array('http_response_code' => $responseInfo['http_code']);//Call failed - } - $this->data = $data; - return $data; - } - - public function getTopStreams(string $pagination = '', int $amount = 25): bool|array|string - { - if ($pagination === '') { - return $this->apiCall('/helix/streams?first=' . rawurlencode($amount) . ''); - } else { - return $this->apiCall('/helix/streams?first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination)); - } - } - - public function getGameTopStreams(int $game_id, string $pagination = '', $amount = 25): bool|array|string - { - if ($pagination === '') { - return $this->apiCall('/helix/streams?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount)); - } else { - return $this->apiCall('/helix/streams?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination)); - } - } - - public function getTopStreamerForGame(): mixed - { - $data = json_decode($this->data, true); - return $data['data'][0]['user_name']; - } - - public function getTopViewersForGame(): mixed - { - $data = json_decode($this->data, true); - return $data['data'][0]['viewer_count']; - } - - public function getTopGames(string $pagination = '', int $amount = 25): bool|array|string - { - if ($pagination === '') { - return $this->apiCall('/helix/games/top?first=' . rawurlencode($amount) . ''); - } else { - return $this->apiCall('/helix/games/top?first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination)); - } - } - - public function getUserDetails(string $username): bool|array|string - { - return $this->apiCall('/helix/users?login=' . rawurlencode($username)); - } - - public function idForUser(): mixed - { - $data = json_decode($this->data, true); - return $data['data'][0]['id']; - } - - public function getUserEmotes(string $username): bool|array|string - { - return $this->apiCall('/api/channels/' . rawurlencode($username) . '/product'); - } - - public function emoteImage(string $emote_id, string $size = '2.0'): string - { - return "https://static-cdn.jtvnw.net/emoticons/v1/$emote_id/$size"; - } - - public function chatForVod(string $vod_id, string $offset): bool|array|string - { - return $this->apiCall('/v5/videos/' . rawurlencode($vod_id) . '/comments?content_offset_seconds=' . rawurlencode($offset) . ''); - } - - public function getUserStream(string $username): bool|array|string - { - return $this->apiCall('/helix/streams?user_login=' . rawurlencode($username) . ''); - } - - public function userIsLive(): ?bool - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0])) { - return false;//No - } else { - return true;//Yes - } - } - - public function streamGameId() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['game_id'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['game_id']; - } - } - - public function streamViewers() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['viewer_count'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['viewer_count']; - } - } - - public function streamTitle() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['title'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['title']; - } - } - - public function streamId() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['id'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['id']; - } - } - - public function streamStart() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['started_at'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['started_at']; - } - } - - public function streamLanguage() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['language'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['language']; - } - } - - public function streamThumbnail() - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['thumbnail_url'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['thumbnail_url']; - } - } - - public function getStreamTags(string $stream_id): bool|array|string - { - return $this->apiCall('/helix/streams/tags?broadcaster_id=' . rawurlencode($stream_id)); - } - - public function getAllStreamTags(string $tag_id): bool|array|string - { - return $this->apiCall('/helix/tags/streams?tag_id=' . rawurlencode($tag_id)); - } - - public function getGameClips(int $game_id, string $pagination = '', int $amount = 25): bool|array|string - { - if ($pagination === '') { - return $this->apiCall('/helix/clips?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount)); - } else { - return $this->apiCall('/helix/clips?game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination) . ''); - } - } - - public function getUserGameClips(string $user_id, int $game_id, string $pagination = '', int $amount = 25): bool|array|string - { - if ($pagination === '') { - return $this->apiCall('/helix/clips?broadcaster_id=' . rawurlencode($user_id) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount)); - } else { - return $this->apiCall('/helix/clips?broadcaster_id=' . rawurlencode($user_id) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount) . '&after=' . rawurlencode($pagination) . ''); - } - } - - public function getUserClips(string $user, int $amount = 25): bool|array|string - { - return $this->apiCall('/helix/clips?broadcaster_id=' . rawurlencode($user) . '&first=' . rawurlencode($amount)); - } - - public function getUserVideos(string $user, string $sort_by, int $amount = 25): bool|array|string - { - if ($sort_by === 'TIME' || $sort_by === 'TRENDING' || $sort_by === 'VIEWS') { - return $this->apiCall('/helix/videos?user_id=' . rawurlencode($user) . '&sort=' . rawurlencode($sort_by) . '&first=' . rawurlencode($amount)); - } else { - return $this->apiCall('/helix/videos?user_id=' . rawurlencode($user) . '&first=' . rawurlencode($amount)); - } - } - - public function getUserVideosForGame(string $user, int $game_id, string $sort_by, int $amount = 25): bool|array|string - { - if ($sort_by === 'TIME' || $sort_by === 'TRENDING' || $sort_by === 'VIEWS') { - return $this->apiCall('/helix/videos?user_id=' . rawurlencode($user) . '&game_id=' . rawurlencode($game_id) . '&sort=' . rawurlencode($sort_by) . '&first=' . rawurlencode($amount)); - } else { - return $this->apiCall('/helix/videos?user_id=' . rawurlencode($user) . '&game_id=' . rawurlencode($game_id) . '&first=' . rawurlencode($amount)); - } - } - - public function getGameData(int $game_id): bool|array|string - { - return $this->apiCall('/helix/games?id=' . rawurlencode($game_id) . ''); - } - - public function gameName(): string - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['name'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['name']; - } - } - - public function gameArtwork(): string - { - $data = json_decode($this->data, true); - if (!isset($data['data'][0]['box_art_url'])) { - throw new Exception("No data found"); - } else { - return $data['data'][0]['box_art_url']; - } - } - - public function getCustom(int $level, string $key): mixed - { - $data = json_decode($this->data, true); - if (!isset($data['data'][$level][$key])) { - throw new Exception("No data found"); - } else { - return $data['data'][$level][$key]; - } - } - - public function setJsonHeader(): void - { - header('Content-Type: application/json'); - } - - public function minutesFormat($minutes, $format = 'H:i:s'): string - { - return gmdate($format, $minutes); - } -} \ No newline at end of file