factorio-item-browser / api-client
Factorio Item Browser 的 API 客户端。
4.1.0
2021-12-04 11:16 UTC
Requires
- php: ^7.4 | ^8.0
- ext-json: *
- bluepsyduck/jms-serializer-factory: ^1.0
- factorio-item-browser/common: ^1.0
- guzzlehttp/guzzle: ^6.3 | ^7.0
- jms/serializer: ^3.2
- laminas/laminas-servicemanager: ^3.0
- symfony/yaml: ^5.0
Requires (Dev)
- bluepsyduck/test-helper: ^2.0
- phpstan/phpstan: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.0
- rregeer/phpunit-coverage-check: ^0.3
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2024-09-21 22:13:09 UTC
README
API 客户端库
此库实现了一个用于访问 Factorio Item Browser 数据的 PHP 客户端。
API 文档可以在以下网址找到:https://docs.factorio-item-browser.com/api
用法
客户端被设置为在 Laminas 项目中使用。在其他上下文中使用它需要额外的设置,这在本 README 中未涵盖。
要使用客户端,将 FactorioItemBrowser\Api\Client\ConfigProvider
添加到您项目的配置聚合器中。
配置
客户端需要以下配置存在于您的项目中
<?php use FactorioItemBrowser\Api\Client\Constant\ConfigKey; return [ ConfigKey::MAIN => [ // The URL to the API server, including a trailing slash. ConfigKey::BASE_URI => 'https://api.factorio-item-browser.com/', // The Api-Key to access the API. ConfigKey::API_KEY => 'foo', // The timeout in seconds to use for the requests. ConfigKey::TIMEOUT => 10, ], ];
示例
实际客户端的用法最好通过示例来描述。
<?php use FactorioItemBrowser\Api\Client\ClientInterface; use FactorioItemBrowser\Api\Client\Request\Item\ItemRandomRequest; use FactorioItemBrowser\Api\Client\Request\Mod\ModListRequest; use FactorioItemBrowser\Api\Client\Response\Item\ItemRandomResponse; use FactorioItemBrowser\Api\Client\Response\Mod\ModListResponse; /* @var \Psr\Container\ContainerInterface $container */ // Fetch the API client from the container. This will use the config to initialize it. /* @var ClientInterface $client */ $client = $container->get(ClientInterface::class); // Create an instance of the request class you want to call, and set its parameters. $randomItemRequest = new ItemRandomRequest(); $randomItemRequest->combinationId = '2f4a45fa-a509-a9d1-aae6-ffcf984a7a76'; $randomItemRequest->locale = 'de'; $randomItemRequest->numberOfResults = 10; $randomItemRequest->numberOfRecipesPerResult = 3; // Send the request to the API server. // This call is non-blocking, returning a Promise. For further details about promises, read the documentation of // the Guzzle HTTP client. $randomItemPromise = $client->sendRequest($randomItemRequest); // Non-blocking // Lets send a second request. $modListRequest = new ModListRequest(); $modListRequest->combinationId = '2f4a45fa-a509-a9d1-aae6-ffcf984a7a76'; $modListRequest->locale = 'de'; $modListPromise = $client->sendRequest($modListRequest); // Non-blocking // To actually process the response, you have to wait on the promises to be fulfilled. /* @var ItemRandomResponse $randomItemsResponse */ $randomItemsResponse = $randomItemPromise->wait(); // Blocking /* @var ModListResponse $modListResponse */ $modListResponse = $modListPromise->wait(); // Blocking // Do something with the received data. var_dump($randomItemsResponse->items); var_dump($modListResponse->mods);