raideer/twitch-api

该包已被废弃,不再维护。未建议替代包。

Twitch.Tv API 包装器(PHP版)

1.1 2016-02-25 07:48 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:55:00 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License StyleCI

PHP版 Twitch API 包装器

  1. 安装
  2. 基本用法
  3. 认证请求
  4. 资源

安装

composer require raideer/twitch-api

用法

基本、未认证请求。

首先,我们需要创建主包装器实例。构造函数只接受一个 Guzzle 客户端作为参数。

$client = new GuzzleHttp\Client;

$wrapper = new Raideer\TwitchApi\Wrapper($client);

现在我们可以通过包装器访问各种 Twitch API 资源。
您可以在这里看到所有资源和它们的方法
https://github.com/raideer/twitch-api/blob/master/resources.md
https://github.com/justintv/Twitch-API#index

/*
 * Wrapper->resource->function()
 */

$response = $wrapper->Channels->getChannel('lirik');
//OR
$response = $wrapper->resource('channels')->getChannel('lirik');

注意:字母的大小写不影响,因此 $wrapper->CHANNELS->getChannel() 仍然有效。

一些方法可以接受一个可选的参数数组。
有关参数列表,请参阅官方 Twitch API 文档。

$wrapper->Channels->getFollows('lirik', ['limit' => 40, 'direction' => 'asc']);

认证请求

首先,我们需要创建一个 OAuth 对象,它将包含认证我们的请求所需的信息。

有关认证的更多信息,请参阅此处

$settings = [
  'client_id'     => 'Your Client ID',
  'client_secret' => 'Your Client Secret',
  'redirect_uri'  => 'Your registered redirect URI',
  'state'         => 'Your provided unique token',
  'scope'         => 'Array or space seperated string of scopes'
];

$oauth = new Raideer\TwitchApi\OAuth($settings);

//Returns the authentication URL
$url = $oauth->getUrl();

一旦用户授权了您的应用程序,他们将被重定向到您指定的 URI,并带有一个代码,该代码是获取访问令牌所必需的。

获取访问令牌

您可以通过使用 getResponse 方法获取访问令牌。如果请求成功,将返回一个 OAuthResponse 对象。它包含访问令牌、刷新令牌和注册的作用域。

$response = $oauth->getResponse($code);

$response->getAccessToken();
$response->getRefreshToken(); //Token refreshing isn't implemented yet
$response->getScope();
$response->hasScope($scope);

现在您可以使用 authorize 方法授权包装器。

//You can pass the OAuthResponse object directly
$wrapper->authorize($response);

//Or just pass the access token
$wrapper->authorize($accessToken);

//Or pass both access token and registered scopes array
$wrapper->authorize($accessToken, $registeredScopes);

如果您只通过传递访问令牌来授权包装器,则包装器将无法检查您试图访问的作用域是否实际存在。(如果您不想进行不必要的请求,这将很有用)

现在您可以进行授权请求。

$wrapper->Channels->getChannel(); //Returns the authenticated user's channel

如果请求超出作用域,将抛出 Raideer\TwitchApi\Exceptions\OutOfScopeException

限制速率(建设中)

如果您需要,您可以使用请求限制。

$wrapper->enableThrottle(true);

请求之间的时间(毫秒)可以设置为如下

// 1 second throttle
$wrapper->setThrottleInterval(1000);

示例

$client = new GuzzleHttp\Client;
$wrapper = new Raideer\TwitchApi\Wrapper($client);

$settings = [
  'client_id'     => 'myClientId',
  'client_secret' => 'myClientSecret',
  'redirect_uri'  => 'https://',
  'state'         => 'myUniqueToken123123',
  'scope'         => ['channel_editor']
];

$oauth = new Raideer\TwitchApi\OAuth($settings);

// You can also add a scope using the addScope method
$oauth->addScope('channel_read');

$url = $oauth->getUrl();
$code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_STRING);
$response = $oauth->getResponse($code);
$wrapper->authorize($response);

$channel = $wrapper->Channels->getChannel();

echo "I'm currently playing " . $channel->game;

资源和它们的方法