bearsys/steam

将多个与Steam相关的库组合成一个封装且易于使用的包。

dev-master 2018-02-11 03:52 UTC

This package is not auto-updated.

Last update: 2024-09-26 15:55:18 UTC


README

本包结合了多个用于处理Steam ID、Steam Web API和Steam oAuth的PHP库。

可以作为独立包使用,也可以作为Nette框架扩展。

包目前包括以下库

安装

composer require bearsys/steam

用法

注意,Valve限制了对此API的请求(如果没有发布者密钥)。每天的请求限制应该是100,000次。这意味着每分钟69次请求。尽管不推荐,但你可能可以一次性请求所有的100,000次。

独立使用

创建BearSys\Steam\SteamWrapper实例,将你的Steam Web API用户密钥作为构造函数参数提供。

然后你可以按需使用此包装器。

示例

$apiKey = 'KEY_GOES_HERE'; // Your API key, matches regex ^[0-9A-F]{32}$
$wrapper = new BearSys\Steam\SteamWrapper($apiKey);

$user = $wrapper->getUser('hitzor'); // you could use any form of Steam ID or vanity URL
$user->getInfo(); // returns info about selected user

$game = $wrapper->getGame(730); // 730 is CS:GO's AppID
$game->getActivePlayersCount(); // returns how many players are currently playing

Nette框架

在配置中注册扩展并设置Steam Web API密钥。

extensions:
    steam: BearSys\Steam\Bridges\NetteDI\SteamExtension

steam:
    apiKey: # your Steam Web API key goes here

之后,你可以在使用Nette DI创建的任何类中请求SteamWrapper。

登录组件

扩展将注册登录工厂。将其注入到你的表示器(或任何其他组件)中,并使用其设置方法设置回调。

/** @var BearSys\Steam\Bridges\NetteApplication\SteamLoginFactory @inject */
public $loginFactory;
// you could use any suitable injection method, this is just the example

public function createComponentSteamLogin()
{
	$successCallback = function (string $steamId, bool $registered) {
		if ($registered)
			$this->redirect('this');
		else
			$this->redirect('User:signUpSteam', $steamId);
	};
	
	$failureCallback = function (\Exception $e) {
		// log $e
	};
	
	return $this->loginFactory->setup(
		function (SteamLogin $login) use ($successCallback, $failureCallback) {
			$this->steamAuthenticator->login($login, $successCallback, $failureCallback);
		}
	);
}

然后你需要在自己的身份验证器中处理登录。你也可以在回调内部做这件事,然后只需使用Nette\Security\User::login()方法以及Nette\Security\IIdentity实例调用它,但我们不建议这样做。

use Ehesp\SteamLogin\SteamLogin;

class SteamAuthenticator extends AbstractAuthenticator
{
	public function login(SteamLogin $login, callable $success, callable $failure)
	{
		try {
			$steamId = $login->validate(); // if this failed, it will throw an exception
			
			/*
			 * Your custom logic goes here.
			 * 
			 * You will probably want to check, if Steam ID is in database.
			 * If it is, then just login associated user.
			 * If it isn't, then create user and redirect him to sign-up page
			 * with pre-filled username from Steam (obtainable using SteamWrapper).
			 */
			 
			$success($steamId, $registered);
		} catch (\Exception $e) {
			// Login attempt failed
			$failure($e);
		}
	}
}

最后,创建到组件处理的链接,并按需设置样式。

<a href="{link steamLogin-steamLogin!}">Steam login</a>

未来计划

  • 测试(尽快)
  • 集成 koraktor/steam-condenser-php
  • 添加对Symfony、Laravel和其他框架的支持(欢迎提交拉取请求)
  • 考虑支持Steamworks发布者API端点