nickbeen/rick-and-morty-api-php

The Rick And Morty API PHP Client

2.0.0 2023-04-25 19:46 UTC

This package is auto-updated.

Last update: 2024-10-18 21:19:10 UTC


README

Latest version Build status Total downloads PHP Version License

An API wrapper written in PHP for the Rick and Morty API at https://rickandmortyapi.com.

Get all the information about characters, episodes and locations of Rick and Morty with this PHP client without messing around with Curl calls and JSON responses. This library does not return the same JSON provided by the API, but wraps everything in convenient PHP class objects. The Rick and Morty API is made by Axel Fuhrmann.

Requirements

  • PHP >= 8.1

Installation

Install the library into your project with Composer.

composer require nickbeen/rick-and-morty-php-api --no-dev

Usage

Initiate a Character, Episode or Location model and use the available tools to list, retrieve or search within that model. The library returns a NotFoundException when using invalid arguments, retrieving nonexistent models or if the Rick and Morty API is not available. Implement a try block to handle the exception in your application in the best possible way.

Collections

Requests for all characters, episodes and locations as well as queries with search parameters will return a Collection object containing an array with results.

Collection object

Info object

Usage

For instructions for the usage of collections, check the sections Characters, Episodes and Locations for more details and examples.

Api

This library contains a useful method to return all the endpoints of the Rick and Morty API in an Api object.

Api object

Usage

Retrieve an Api object containing the endpoints for retrieving characters, episodes and locations.

$api = new Api();
$api->get();

echo $api->characters;
echo $api->episodes;
echo $api->locations;

Characters

Retrieve any character from Rick and Morty, browse through all the characters or use search parameters to find a specific character.

Character object

Origin object

Location object

List characters

Retrieve a Collection object with next 20 characters from the first page.

$characters = new Character();
$characters->get();

foreach ($characters->results as $character) {
    echo $character->name;
}

Retrieve a Collection object with next 20 characters from the second page.

$characters = new Character();
$characters->page(2)
    ->get();

foreach ($characters->results as $character) {
    echo $character->name;
}

Get character

Retrieve a Character object with id 1.

$character = new Character();
$character->get(1);

echo $character->name;

Get multiple characters

Retrieve an array containing Character objects with id 1 and 2.

$characters = new Character();
$characters->get(1,2);

foreach ($characters as $character) {
    echo $character->name;
}

Search characters

It is possible to search for characters based on search parameters such as species and name. For gender and status you can use a Gender enum object and Status enum object for auto-completion in an IDE.

Gender object

Status object

Retrieve a Collection with all alive male Ricks.

$characters = new Character();
$characters->withGender(Gender::Male)
    ->withName('Rick')
    ->withStatus(Status::Alive)
    ->get();

foreach ($characters->results as $character) {
    echo $character->name;
}

Episodes

Retrieve any episode from Rick and Morty, browse through all the episodes or use search parameters to find a specific episode.

Episode object

List episodes

Retrieve a Collection object with first 20 episodes from the first page.

$episodes = new Episode();
$episodes->get();

foreach ($episodes->results as $episode) {
    echo $episode->name;
}

Retrieve a Collection object with next 20 episodes from the second page.

$episode = new Episode();
$episode->page(2)
    ->get();

foreach ($episodes->results as $episode) {
    echo $episode->name;
}

Get episode

Retrieve an Episode object with id 1.

$episode = new Episode();
$episode->get(1);

echo $episode->name;

Get multiple episodes

Retrieve an array containing Episode objects with id 1 and 2.

$episodes = new Episode();
$episodes->get(1,2);

foreach ($episodes as $episode) {
    echo $episode->name;
}

Search episodes

It is possible to search for episodes based on search parameters such as episode code (e.g. S01E01) and name.

Retrieve a Collection object with all episodes from season 1.

$episodes = new Episode();
$episodes->withEpisode('S01')
    ->get();

foreach ($episodes->results as $episode) {
    echo $episode->name;
}

Locations

Retrieve any location from Rick and Morty, browse through all the featured locations or use search parameters to find a specific location.

Location object

List locations

Retrieve a Collection object with first 20 locations from the first page.

$locations = new Location();
$locations->get();

foreach ($locations->results as $location) {
    echo $location->name;
}

Retrieve a Collection object with next 20 locations from the second page.

$locations = new Location();
$locations->page(2)
    ->get();

foreach ($locations->results as $location) {
    echo $location->name;
}

Get location

Retrieve a Location object with id 1.

$location = new Location();
$location->get(1);

echo $location->name;

Get multiple locations

Retrieve an array containing Location objects with id 1 and 2.

$locations = new Location();
$locations->get(1,2);

foreach ($locations as $location) {
    echo $location->name;
}

Search locations

It is possible to search for locations based on search parameters such as dimension and name.

Retrieve a Collection object with all locations within dimension C-137.

$location = new Location();
$location->withDimension('Dimension C-137')
    ->get();

foreach ($locations->results as $location) {
    echo $location->name;
}

FAQ

What are the limits of the API?

The API is open and requires no authentication. According to the R implementation Mortyr, the API allows 10.000 requests per IP address per day after which any request will result in a 429 (Too Many Requests) response.

Can I get more than 20 results per page in a Collection?

The API returns 20 results per page by design and this cannot be adjusted. It is however possible to return more than 20 results by manually inserting more than 20 ids in the get() method.

How about extra filters?

Not possible, this library already offers all the available filters currently provided by the API.

Why is the code of the episode called episode in the Episode model?

It looks like an oopsie from the creator of the Rick And Morty API. There is an unresolved pull request to rename the field to code.

License

This library is licensed under the MIT License (MIT). See the LICENSE for more details.