michaelgarrez/lol-api

《英雄联盟》API 的包装器

v3.0.0 2020-04-23 11:57 UTC

README

Scrutinizer Code Quality Code Coverage Build Status Build Status SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

介绍

简单 PHP 包装器,用于 《英雄联盟》API

此库实现了两个自定义异常来捕获您的 API 速率限制(ServiceRateLimitException 和 UserRateLimitException)。

它还实现了 Doctrine 缓存,以便将 API 结果缓存到您喜欢的缓存驱动器中。

从 0.* 迁移到 1.*

三个主要功能导致 BC 破坏,导致版本升级到 1.*

  • 缓存实现
  • AbstractRateLimitException
  • 返回 ApiResult 对象而不是数组

只有第三个实际上会破坏 BC。您现在应该在返回的 ApiResult 对象上使用 getResult() 方法。

如何使用

基本使用

您首先必须选择您想要获取的 API 以及特定的方法。每次调用都将返回一个包含调用 URL、Guzzle 响应对象以及包含 API 结果的数组的 ApiResult 对象。

要获取结果,您可以在 ApiResult 对象上调用 getResult() 方法。

$apiClient = new \LoLApi\ApiClient(\LoLApi\ApiClient::REGION_EUW, 'my-key');

$apiClient->getMatchApi()->getMatchListBySummonerId(2);
$apiClient->getMatchApi()->getMatchByMatchId(2, true);
$apiClient->getSummonerApi()->getSummonerBySummonerName('MySummonerName');
$apiClient->getSummonerApi()->getSummonerBySummonerId(2);
$apiClient->getMasteriesApi()->getMasteriesBySummonerId(2);
$apiClient->getRunesApi()->getRunesBySummonerId(2);
$apiClient->getSummonerApi()->getSummonerNameBySummonerId(2);
$apiClient->getChampionApi()->getChampionById(20);
$apiClient->getFeaturedGamesApi()->getFeaturedGames();
$apiClient->getStatsApi()->getRankedStatsBySummonerId(2);
$apiClient->getGameApi()->getRecentGamesBySummonerId(2);
$apiClient->getSpectatorApi()->getCurrentGameByPlatformIdAndSummonerId('EUW1', 2);

使用缓存

默认情况下使用 Symfony NullAdapter 缓存。您可以将其他 Cache Adapter(实现 PSR6 Adapters)指定给 ApiClient。

使用 Predis 的示例

use Symfony\Component\Cache\Adapter\RedisAdapter;

$client = new \Predis\Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]);

$redisAdapter = new RedisAdapter($client);

$apiClient->setCacheProvider($redisAdapter);

// This will call the API and return to you an ApiResult object
$result = $apiClient->getSummonerApi()->getSummonerBySummonerName('MySummonerName');

// Let's cache this result for 60 seconds into Redis
$apiClient->cacheApiResult($result, 60);

// This will fetch the data from Redis and return to you an ApiResult object
$result = $apiClient->getSummonerApi()->getSummonerBySummonerName('MySummonerName');

默认 ttl 值为 cacheApiResult() 方法的 60 秒。

速率限制

当您达到速率限制(用户或服务)时,库将抛出一个 AbstractRateLimitException 的实现。您可以获取速率限制类型以及在新调用之前等待的时间(Riot 对速率限制的尊重非常严格)。

带有睡眠的示例

$apiClient = new \LoLApi\ApiClient(\LoLApi\ApiClient::REGION_EUW, 'my-key');

for ($i = 0; $i < 100; $i++) {
    try {
        $apiClient->getMatchApi()->getMatchListByAccountId(2);
    } catch (AbstractRateLimitException $e) {
        sleep($e->getRetryAfter());
    }
}

实现的 API

贡献

如果您在此库集成中遇到任何问题,请创建问题。

如果您想做出贡献,请创建 PR,您必须遵守 PSR-2,并且您的代码必须经过测试。

谢谢!