radiojoe / twitch-api-php
PHP 的 Twitch API 客户端。
Requires
- php: >=7.4.0
- ext-json: *
- guzzlehttp/guzzle: ~6.0|~7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.10
- phpspec/phpspec: ^7.2
- phpunit/phpunit: ^9.5
- dev-main
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.6
- v7.1.4
- v7.1.2
- v7.1.1
- v6.1.0
- v6.0.0
- v5.0.5
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.0
- v3.3.0
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.2.2
- v1.2.1
- v1.2
- v1.1
- v1.0
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-feature/eventsub_raid_and_beta_updates
- dev-feature/channel-follow-version-update
- dev-feature/composer-branch
- dev-feature/goals
This package is auto-updated.
Last update: 2024-09-25 23:16:04 UTC
README
这是从 https://github.com/nicklaw5/twitch-api-php 分支出来的版本
Twitch API PHP 库
Twitch API PHP 库允许您通过 HTTP 与多个 Twitch API 端点进行交互。该库不会格式化您的请求的响应,因此您可以在如何处理从 API 返回的数据方面有完全的灵活性。
文档与链接
入门
要求
- PHP 7.4 - 该库已在早期版本上进行了测试,但我们鼓励您使用与我们的库一起测试的最新版本的 PHP。 - 未来要求将提高至 PHP 8.0,因此您应该为 PHP 的最新版本进行开发。
- Composer
ext-json: *- guzzlehttp/guzzle
~6.0|~7.0
安装
推荐通过 Composer 安装 Twitch API PHP 库。
composer require nicklaw5/twitch-api-php
示例用法
调用 Twitch API 所有调用都需要通过 OauthApi 类检索的承载令牌。您可以在 Twitch API 文档中查看 令牌类型。以下示例直接在示例中存储客户端 ID、密钥和作用域,但您不应这样做。将您的 ID、密钥和作用域存储在安全的地方,例如您的数据库、环境变量或替代设置存储。此信息的安全性很重要。以下是如何为您的应用程序检索令牌的示例
$twitch_client_id = 'TWITCH_CLIENT_ID'; $twitch_client_secret = 'TWITCH_CLIENT_SECRET'; $twitch_scopes = ''; $helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id); $twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret); $oauth = $twitchApi->getOauthApi(); try { $token = $oauth->getAppAccessToken($twitch_scopes ?? ''); $data = json_decode($token->getBody()->getContents()); // Your bearer token $twitch_access_token = $data->access_token ?? null; } catch (Exception $e) { //TODO: Handle Error }
以下是检索用户令牌的示例
$twitch_client_id = 'TWITCH_CLIENT_ID'; $twitch_client_secret = 'TWITCH_CLIENT_SECRET'; $twitch_scopes = ''; $helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id); $twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret); $oauth = $twitchApi->getOauthApi(); // Get the code from URI $code = $_GET['code']; // Get the current URL, we'll use this to redirect them back to exactly where they came from $currentUri = explode('?', 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])[0]; if ($code == '') { // Generate the Oauth Uri $oauthUri = $oauth->getAuthUrl($currentUri, 'code', $twitch_scopes); // Redirect them as there was no auth code header("Location: {$oauthUri}"); } else { try { $token = $oauth->getUserAccessToken($code, $currentUri); // It is a good practice to check the status code when they've responded, this really is optional though if ($token->getStatusCode() == 200) { // Below is the returned token data $data = json_decode($token->getBody()->getContents()); // Your bearer token $twitch_access_token = $data->access_token ?? null; } else { //TODO: Handle Error } } catch (Exception $e) { //TODO: Handle Error } }
当用户令牌过期时,您可以通过刷新它而不是要求他们再次进行身份验证来刷新它。以下是刷新用户令牌的示例
$twitch_client_id = 'TWITCH_CLIENT_ID'; $twitch_client_secret = 'TWITCH_CLIENT_SECRET'; $twitch_scopes = ''; $user_refresh_token = 'REFRESH_TOKEN'; $helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id); $twitchApi = new \TwitchApi\TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret); $oauth = $twitchApi->getOauthApi(); try { $token = $oauth->getAppAccessToken($twitch_scopes ?? ''); $data = json_decode($token->getBody()->getContents()); // Your bearer token $twitch_access_token = $data->access_token ?? null; // The scopes from the API $twitch_scopes = $data->scope; } catch (Exception $e) { //TODO: Handle Error }
API 类的使用
一切从 TwitchApi 类开始。然而,如果您想单独实例化 UsersApi、OauthApi 等,您当然可以这样做。
API 调用通常返回一个实现 ResponseInterface 的对象。由于您获取了完整的 Response 对象,您需要处理其内容,例如使用 json_decode() 将其解码为对象。此库不假设您想这样做,因此不会为您自动执行此操作。此库只是在您的代码和 Twitch 之间充当中间人,为您提供 Twitch API 返回的原始响应。
从 TwitchApi 可以调用的单个 API 类对应于 Twitch API 文档。其余的 API 类基于此处列出的资源 这里。类中的方法通常对应于每个资源的端点。命名约定是为了尽可能匹配 Twitch 文档。每个主要端点方法(不是便利或辅助方法)都应该有一个 @link 注解,指向该端点的具体文档的 URL。
以下是使用访问令牌检索用户表的示例
$twitch_client_id = 'TWITCH_CLIENT_ID'; $twitch_client_secret = 'TWITCH_CLIENT_SECRET'; // Assuming you already have the access token - see above $twitch_access_token = 'the token'; // The Guzzle client used can be the included `HelixGuzzleClient` class, for convenience. // You can also use a mock, fake, or other double for testing, of course. $helixGuzzleClient = new \TwitchApi\HelixGuzzleClient($twitch_client_id); // Instantiate TwitchApi. Can be done in a service layer and injected as well. $twitchApi = new TwitchApi($helixGuzzleClient, $twitch_client_id, $twitch_client_secret); try { // Make the API call. A ResponseInterface object is returned. $response = $twitchApi->getUsersApi()->getUserByAccessToken($twitch_access_token); // Get and decode the actual content sent by Twitch. $responseContent = json_decode($response->getBody()->getContents()); // Return the first (or only) user. return $responseContent->data[0]; } catch (GuzzleException $e) { //TODO: Handle Error }
开发者工具
PHP 代码标准修复器
PHP编码标准修复工具(php-cs-fixer)已被添加,专门用于新Twitch API代码。该配置文件位于.php_cs.dist中。规则集设置为默认(目前为PSR-2)。配置文件主要仅将作用范围限制在新Twitch API代码上。
您可以使用vendor/bin/php-cs-fixer fix命令运行修复工具。然而,使用提供的git钩子是运行修复工具的最简单方法。
Git预提交钩子
在bin/git/hooks目录下,您可以找到一个pre-commit钩子,将其添加到git中,每次提交时都会自动运行php-cs-fixer。结果是,提交完成后,修复工具所做的任何更改都作为未暂存的更改保留。您可以审查这些更改,然后添加并提交它们。
要安装钩子,请转到.git/hooks目录并执行ln -s ../../bin/git/hooks/pre-commit。
许可证
在MIT许可证下分发。