da-mitchell / steam-api
Steam Web API 的 PHP 封装
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ~6.1
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.6
This package is auto-updated.
Last update: 2024-09-23 19:56:31 UTC
README
Steam API 的 PHP 封装
很乐意听到正在积极使用此工具的人的消息。这里有一个 Gitter 的链接 https://gitter.im/DaMitchell/steam-api-php。
这是库的 v2 版本,它基本上是一个重写,使其更加灵活。它将允许您对响应进行任何操作,无论是获取数组还是将响应映射到对象。
我将所有可用的命令基于以下文档 https://lab.xpaw.me/steam_api_documentation.html。
安装
使用 Composer 安装最新版本,运行 composer require da-mitchell/steam-api
使用方法
<?php use GuzzleHttp\Client; use Steam\Configuration; use Steam\Runner\GuzzleRunner; use Steam\Runner\DecodeJsonStringRunner; use Steam\Steam; use Steam\Utility\GuzzleUrlBuilder; $steam = new Steam(new Configuration([ Configuration::STEAM_KEY => '<insert steam key here>' ])); $steam->addRunner(new GuzzleRunner(new Client(), new GuzzleUrlBuilder())); $steam->addRunner(new DecodeJsonStringRunner()); /** @var array $result */ $result = $steam->run(new \Steam\Command\Apps\GetAppList()); var_dump($result);
配置
可以将两个参数传递给 Configuration
对象
- steam_key,您可以从 http://steamcommunity.com/dev/apikey 获取的 API 密钥。
- base_steam_api_url,一个可选参数,用于覆盖
http://api.steampowered.com
作为基本 API URL。
如上图所示,您可以通过传递给 Configuration
构造函数来设置 Steam API 密钥
$steam = new Steam(new Configuration([ Configuration::STEAM_KEY => '<insert steam key here>' ]));
命令
命令是本质上描述每个端点的类。每个命令都实现了 Steam\Command\CommandInterface
并具有方法,这些方法将给出运行器的接口、方法、版本、HTTP 方法以及端点所需的任何参数。
我已实现所有 GET 端点的所有命令。我不太确定要实现哪些 POST 端点,因为我并不完全清楚它们是如何工作的。所以如果有人理解它们,请实现它们并提交 PR,我将添加它们。
运行器
因此,运行器是相当简单的对象,它们实现了 Steam\Runner\RunnerInterface
,该接口有 3 个方法,其中最重要的方法是 run
。其他两个用于设置配置对象。
run 方法有两个参数,$command
和 $result
。显然,$command
是您请求的端点,而 $result
是上一个运行器的结果。这意味着第一个附加的运行器的 $result
将为 null。
测试
从项目根目录运行测试,使用 php vendor/bin/phpunit