dsbilling / phpokeapi
一个用于PokéAPI的PHP包装器,具有缓存和懒加载功能
dev-master
2022-12-29 16:08 UTC
Requires
- php: ^7.1|^8.0
- doctrine/common: ^2.8|^3.0
- jms/serializer: ^1.11|^3.11
- ocramius/proxy-manager: ^2.1|^2.14
- psr/cache: ^1.0|^3.0
- psr/simple-cache: ^1.0|^3.0
- symfony/cache: ^4.0|^6.0
- symfony/yaml: ^4.0|^6.0
Requires (Dev)
- phpunit/phpunit: ^7.2|^8.0
This package is auto-updated.
Last update: 2024-09-29 06:15:01 UTC
README
一个PokéAPI的PHP包装器。此包提供查询PokéAPI大多数端点的能力(以下为例外)。它支持响应缓存和关系懒加载。
安装
composer require dsbilling/phpokeapi
基本用法
使用PokeAPI\Client通过命名方法直接查询端点。
<?php use PokeAPI\Client; $client = new Client(); // Returns a PokeAPI\Pokemon\Species instance $species = $client->species('bulbasaur'); // or $client->species(1);
然后您可以遍历返回的对象。所有关系都将作为代理,除非您显式调用它们的getter之一,否则不会向API发送新的请求
<?php // ... $species->getName(); // 'bulbasaur' $growthRate = $species->getGrowthRate(); // A proxy of PokeAPI\Pokemon\GrowthRate $growthRate->getName(); // Here the real API call to the GrowthRate endpoint is made
所有请求都将被缓存,因此您不需要查询相同的数据集两次。
PokeAPI\Client
PokeAPI\Client接受3个可选参数
$url,指向PokéAPI基础URL的字符串。默认为pokeapi.co$cache,一个Psr\SimpleCache\CacheInterface。默认为一个Symfony\Component\Cache\Simple\FilesystemCache实例$serializer,一个JMS\Serializer\SerializerInterface实现。
贡献
请随意提交pull请求或提交问题!