Home

Awesome

The Rick and Morty API - PHP Client

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

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

FieldTypeDescription
infoInfoInformation about the collection
resultsCharacter[]<br>Episode[]<br>Location[]Array containing characters, episodes or locations

Info object

FieldTypeDescription
countintegerThe length of the response
pagesintegerThe amount of pages
next?stringLink to the next page (if it exists)
prev?stringLink to the previous page (if it exists)

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

FieldTypeDescription
charactersstringEndpoint used for retrieving characters
episodesstringEndpoint used for retrieving episodes
locationsstringEndpoint used for retrieving locations

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

FieldTypeDescription
idintegerThe id of the character
namestringThe name of the character
statusstringThe status of the character
speciesstringThe species of the character
typestringThe type or subspecies of the character
genderstringThe gender of the character
originOriginName and link to the character's origin location endpoint
locationLocationName and link to the character's last known location endpoint
imagestringLink to the character's image
episodestring[]List of episodes in which this character appeared
urlstringLink to the character's own URL endpoint
createdstringTime at which the character was created in the database

Origin object

FieldTypeDescription
namestringThe name of the character
urlstringUrl to the character's last known location endpoint

Location object

FieldTypeDescription
namestringThe name of the character's origin location
urlstringUrl to the character's origin location endpoint

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.

MethodTypeDescription
withGenderGenderFilter by given gender
withNamestringFilter by given name
withSpeciesstringFilter by given species
withStatusStatusFilter by given status
withTypestringFilter by given type

Gender object

FieldTypeDescription
Gender::FemalestringFemale
Gender::GenderlessstringGenderless
Gender::MalestringMale
Gender::UnknownstringUnknown

Status object

FieldTypeDescription
Status::AlivestringAlive
Status::DeadstringDead
Status::UnknownstringUnknown

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

FieldTypeDescription
idintegerThe id of the episode
namestringThe name of the episode
air_datestringThe air date of the episode
episodestringThe code of the episode
charactersstring[]List of characters who have been seen in the episode
urlstringLink to the episode's own URL endpoint
createdstringTime at which the episode was created in the database

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.

MethodTypeDescription
withEpisodestringFilter by given episode code
withNamestringFilter by given 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

FieldTypeDescription
idintegerThe id of the location
namestringThe name of the location
typestringThe type of the episode
dimensionstringThe dimension in which the location is located
residentsstring[]List of characters who have been seen in the location
urlstringLink to the location's own URL endpoint
createdstringTime at which the location was created in the database

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.

MethodTypeDescription
withDimensionstringFilter by given dimension
withNamestringFilter by given name
withTypestringFilter by the given type

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.