lmerotta/phpokeapi

一个用于PokéAPI的PHP7.1包装器,具有缓存和懒加载功能

1.1.5 2019-09-27 06:37 UTC

This package is auto-updated.

Last update: 2024-09-27 18:12:10 UTC


README

A PHP7.1+ wrapper for PokéAPI. This package offers the possibility to query the majority of the PokéAPI endpoints (see exceptions below). It supports caching of responses and lazy-loading relations.

安装

composer require lmerotta/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实现。

贡献

请随意打开拉取请求或提交问题!