squegtech / steam-api
Steam网络API的PHP包装器
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.4
Requires (Dev)
- mockery/mockery: ^1.5
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-24 20:40:28 UTC
README
A PHP wrapper for the Steam API. Updated and maintained for the latest versions of PHP and Guzzle.
This package replaces https://github.com/DaMitchell/steam-api-php which has been abandoned.
All existing commands are based on this documentation: https://steamapi.xpaw.me/.
安装
使用 Composer 安装最新版本,运行 composer require squegtech/steam-api
用法
<?php include_once __DIR__ . '/../vendor/autoload.php'; use GuzzleHttp\Client; use SquegTech\Steam\Command\Apps\GetAppList; use SquegTech\Steam\Configuration; use SquegTech\Steam\Runner\GuzzleRunner; use SquegTech\Steam\Runner\DecodeJsonStringRunner; use SquegTech\Steam\Steam; use SquegTech\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 GetAppList()); var_dump($result);
配置
可以向 Configuration
对象传递两个参数
- steam_key,您可以从 https://steamcommunity.com/dev/apikey 获取的API密钥。
- base_steam_api_url,一个可选参数,用于覆盖
https://api.steampowered.com
作为基本API URL。
如上图所示,您可以通过传递给 Configuration
构造函数来设置Steam API密钥
$steam = new Steam(new Configuration([ Configuration::STEAM_KEY => '<insert steam key here>' ]));
命令
命令是描述每个端点的类。每个命令实现了 SquegTech\Steam\Command\CommandInterface
,并具有提供执行者接口、方法、版本、HTTP方法和端点所需的任何参数的方法。
大多数命令是为GET端点。POST端点尚未完全实现,请提交PR以添加您想要的那些。
执行者
执行者是实现了 SquegTech\Steam\Runner\RunnerInterface
的简单对象。此接口有3个方法,其中最重要的是 run
方法。其他两个用于设置配置对象。
run方法有两个参数,$command
和 $result
。 $command
是您请求的端点,$result
是前一个执行者的结果。这意味着第一个附加的执行者的 $result
将为null。
Docker
此项目包含一个Docker镜像和Docker Compose环境,可用于运行。确保已安装并使用 docker-compose up -d
启动它。
一旦Docker环境启动,您可以使用 docker-compose exec squegtech-steam-api bash
访问其命令行。
测试
在Docker环境外部或在其bash shell内部,从项目根目录运行测试: docker-compose exec squegtech-steam-api vendor/phpunit/phpunit/phpunit