iarcadia / php-magic-pokeapi
An awesome PokéAPI PHP wrapper.
Requires
- php: >=7.1.7
- symfony/console: ^4.0
- symfony/yaml: ^4.0
This package is auto-updated.
Last update: 2024-10-29 04:20:13 UTC
README
This package is a POO-oriented PHP wrapper for using the version 2 of the RESTful API PokéAPI. With MagicPokeAPI, you can obtain tons of data from PokéAPI server, thanks to some lines. The package includes an useful mini caching system.
What's PokéAPI?
This is a full RESTful API linked to an extensive database detailing everything about the Pokémon main game series.
Installation
Require this package with composer:
composer require iarcadia/php-magic-pokeapi
Configuration
First of all, you need to check files into the config
folder.
cache.php
is cache systems related.
request.php
is request systems related.
lang.php
is language systems related.
Please, respect the comments written in these files.
Usage
First step
Yep, you probably guess right: you have to create an instance of your favorite PokéAPI wrapper in order to continue.
$api = new PokeAPI();
Don't forget to include
files or to use
classes. (e.g. use iArcadia\MagicPokeAPI\PokeAPI
)
Second step
You must specify the resource that you want to look for! Use the resource()
method.
$api->resource('pokemon'); // or (and the recommended one) $api->resource(PokeAPI::RESOURCE_POKEMON);
The PokeAPI
class provides constants for all resources. It could be a good idea to use them instead of direct string.
A constant list is available in src/PokeAPI.php
file.
Endpoints
Setting the number of results (or "limit")
Use the limit()
method.
$api->limit(20);
Setting the search starting point (or "offset")
Use the offset()
or the skip()
method.
$api->offset(5); // or $api->skip(5);
The skip()
method is an alias for the offset()
one.
Executing the request
Use the get()
method.
$data = $api->get();
Resource details
Very simple, just use the find()
method.
// by name $item = $api->find('arcanine'); // or by id $item = $api->find(25);
Other
Requesting raw URLs
You want to directly write your URL? Use the raw()
method.
// endpoint $api->raw('/ability/'); // with parameters $api->raw('/pokemon-species/?limit=30&offset=60'); // resource details $api->raw('/pokemon/bulbasaur/'); // with full URL $api->raw('https://www.pokeapi.co/api/v2/item/203');
You can specify many URLs with an array, or with many arguments.
// array way $api->raw(['/ability/', 'https://www.pokeapi.co/api/v2/item/203']); // many args way $api->raw('/ability/', 'https://www.pokeapi.co/api/v2/item/203');
Note that after using it, your PokeAPI
object properties (url, resource, limit and offset) will be updated from your raw URL (or the last one if you specifies many URLs).
Forcing the cache update
If a reason, you want to force the update of a cached file, use the cacheForcing()
method.
$api->cacheForcing(true);
It will update the cache for the next request ONLY.
Tips
Chaining methods
Thanks to the power of the POO, you can quickly set up options between two different requests.
// for endpoint $api->limit(20)->offset(60)->resource(PokeAPI::RESOURCE_ITEM)->get(); // for resource details $api->resource(PokeAPI::RESOURCE_ITEM)->find('potion');
Constructor options
If you prefer, you can also set up options at the instance creation (if you know that they won't change for example).
$api = new PokeApi( [ 'limit' => 20, 'offset' => 0, // works with "skip" too 'resource' => PokeAPI::RESOURCE_CONTEST_EFFECT ]);
Using automatic resource name translation
/!\ CURRENTLY ONLY WORK WITH POKEMON NAME /!\
If you decide to activate the automatic resource name translation (in the config/lang.php
file), you will be able to use your language name for requesting data!
// Charizard in french will be translated to: // $api->resource(PokeAPI::RESOURCE_POKEMON)->find('charizard'); $api->resource(PokeAPI::RESOURCE_POKEMON)->find('dracaufeu'); // Nidoqueen in korean will be translated to: // $api->resource(PokeAPI::RESOURCE_POKEMON)->find('nidoqueen'); $api->resource(PokeAPI::RESOURCE_POKEMON)->find('니드퀸');
Bonus: even if you use this feature, all english will continue to work!
CHANGELOGS
See CHANGELOGS.md