obnoxiousfrog/twitch-api

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

v1.0.0 2015-06-01 02:23 UTC

This package is not auto-updated.

Last update: 2020-01-21 17:22:51 UTC


README

这是一个用于简化与Twitch API工作的PHP库。

请阅读

我注意到,这个包可能不再与最近的Twitch API更改兼容。不幸的是,我现在没有时间深入研究它。希望有一天在不太遥远的将来,我甚至可能完全重写它。在此之前,我不会提供对任何重大功能请求或问题的支持。

如果您决定使用和/或依赖这个包,请记住这一点。

谢谢。

安装

通过运行以下 Composer 命令进行安装

composer require obnoxiousfrog/twitch-api

示例

以下是一个示例,用于验证用户是否正在关注并订阅用户 obnoxiousfrog,如果是,他们将收到一条消息。如果不是,将抛出异常以记录日志,并将用户重定向到另一个带有错误消息的页面。

<?php require_once 'vendor/autoload.php';

try {

	$auth = new TwitchApi\Authenticator(new TwitchApi\Application(
		'3nyv5fcqdpg8uakewz6mj27r49xhbs',
		'aqdrje795sh4nm2fk8btpwuvy3cg6z',
		['user_read', 'user_subscriptions'],
		'https://www.domain.tld/authenticate'
	));
	
	if ( ! $auth->authenticate() or ! ($user = $auth->user()))
	{
		throw new Exception('Oops, something went wrong!');
	}
	
	if ( ! $user->isFollowing('obnoxiousfrog'))
	{
		throw new Exception('You are not a follower of ObnoxiousFrog!');
	}
	
	if ( ! $user->isSubscribed('obnoxiousfrog'))
	{
		throw new Exception('You are not a subscriber of ObnoxiousFrog!');
	}
	
	echo 'Welcome, '.$user->displayname().'!';

} catch (Exception $e) {
	// Log error
}

文档

TwitchApi\Application

此类实现了 \TwitchApi\Contracts\Application 协议。

整个API都围绕 Application 对象展开。

$twitch = new TwitchApi\Application('client', 'secret', ['scopes'], 'redirect');

client 是您的Twitch应用程序客户端ID。

secret 是您的Twitch应用程序客户端密钥(不要与任何人分享)。

scopes 是Twitch作用域的数组。

redirect 是Twitch应用程序的重定向URL。

以下是在生产环境中可能的样子。

$twitch = new TwitchApi\Application(
	'3nyv5fcqdpg8uakewz6mj27r49xhbs',
	'aqdrje795sh4nm2fk8btpwuvy3cg6z',
	['user_read', 'user_subscriptions'],
	'https://www.domain.tld/authenticate',
);

类方法

可见性 方法 参数 返回
public __construct string $client string $secret array $scopes string $redirect void
public request - \TwitchApi\Contracts\Request
public bind string $abstract, string $concrete void
public instance string $abstract, array $parameters [] \StdClass
public make string $abstract, array $parameters [] \StdClass
private instantiate string $concrete, array $parameters \StdClass
public static api - string
public static client - string
public static secret - string
public static scopes - string
public static redirect - string

TwitchApi\Authenticator

此类实现了 \TwitchApi\Contracts\Authenticator 协议。

$auth = new TwitchApi\Authenticator($twitch);

类方法

可见性 方法 参数 返回
public __construct \TwitchApi\Contracts\Application $app void
public authenticate - boolean
public user - \TwitchApi\Contracts\User
public channel string $channel \TwitchApi\Contracts\Channel
public static url string $client null, string $redirect null, string $scopes null string

TwitchApi\User

此类实现了 \TwitchApi\Contracts\User 协议。

类方法

可见性 方法 参数 返回
public __construct \TwitchApi\Contracts\Application $app, \TwitchApi\Contracts\Response $token, \TwitchApi\Contracts\Response $user void
public accessToken - string
public refreshToken - string
public id - integer
public email - string
public name - string
public displayname - string
public 标志 - string
public 个人简介 - string
public 时间戳 - 数组
public 是否合作 - boolean
public 是否订阅 字符串 $streamer boolean
public 是否关注 字符串 $streamer boolean

TwitchApi\Channel

该类实现了 \TwitchApi\Contracts\Channel 协议。

类方法

可见性 方法 参数 返回
public __construct \TwitchApi\Contracts\Application $app, \TwitchApi\Contracts\User $user, string $channel void
public 获取 - \TwitchApi\Contracts\Response
public 团队 - 数组
public 表情符号 - 数组

TwitchApi\Request

该类实现了 \TwitchApi\Contracts\Request 协议。

类方法

可见性 方法 参数 返回
public __construct \TwitchApi\Contracts\Application $app void
public 获取 string $endpoint, array $parameters [], array $headers [] \TwitchApi\Contract\Response
public 发布 string $endpoint, array $parameters [], array $headers [] \TwitchApi\Contract\Response

TwitchApi\Response

该类实现了 \TwitchApi\Contracts\Response 协议。

类方法

可见性 方法 参数 返回
public __construct \TwitchApi\Contracts\Application $app, string $response, array $request void
public __get string $property mixed

使用自己的类

请确保您的类实现了适当的协议(您可以在每个类文档的标题下方找到所有协议),然后您只需从 \TwitchApi\Application 对象中运行 bind 方法。

例如,以下将替换 TwitchApi User 类为您自己的类

$twitch->bind('TwitchApi\Contracts\User', 'CustomUserClass');