承接 nickbeen/rick-and-morty-api-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nickbeen/rick-and-morty-api-php

Composer 安装命令:

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

包简介

The Rick And Morty API PHP Client

README 文档

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

Field Type Description
info Info Information about the collection
results Character[]
Episode[]
Location[]
Array containing characters, episodes or locations

Info object

Field Type Description
count integer The length of the response
pages integer The amount of pages
next ?string Link to the next page (if it exists)
prev ?string Link 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

Field Type Description
characters string Endpoint used for retrieving characters
episodes string Endpoint used for retrieving episodes
locations string Endpoint 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

Field Type Description
id integer The id of the character
name string The name of the character
status string The status of the character
species string The species of the character
type string The type or subspecies of the character
gender string The gender of the character
origin Origin Name and link to the character's origin location endpoint
location Location Name and link to the character's last known location endpoint
image string Link to the character's image
episode string[] List of episodes in which this character appeared
url string Link to the character's own URL endpoint
created string Time at which the character was created in the database

Origin object

Field Type Description
name string The name of the character
url string Url to the character's last known location endpoint

Location object

Field Type Description
name string The name of the character's origin location
url string Url 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.

Method Type Description
withGender Gender Filter by given gender
withName string Filter by given name
withSpecies string Filter by given species
withStatus Status Filter by given status
withType string Filter by given type

Gender object

Field Type Description
Gender::Female string Female
Gender::Genderless string Genderless
Gender::Male string Male
Gender::Unknown string Unknown

Status object

Field Type Description
Status::Alive string Alive
Status::Dead string Dead
Status::Unknown string Unknown

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

Field Type Description
id integer The id of the episode
name string The name of the episode
air_date string The air date of the episode
episode string The code of the episode
characters string[] List of characters who have been seen in the episode
url string Link to the episode's own URL endpoint
created string Time 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.

Method Type Description
withEpisode string Filter by given episode code
withName string Filter 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

Field Type Description
id integer The id of the location
name string The name of the location
type string The type of the episode
dimension string The dimension in which the location is located
residents string[] List of characters who have been seen in the location
url string Link to the location's own URL endpoint
created string Time 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.

Method Type Description
withDimension string Filter by given dimension
withName string Filter by given name
withType string Filter 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.

nickbeen/rick-and-morty-api-php 适用场景与选型建议

nickbeen/rick-and-morty-api-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131 次下载、GitHub Stars 达 12, 最近一次更新时间为 2022 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「api」 「rick and morty」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nickbeen/rick-and-morty-api-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nickbeen/rick-and-morty-api-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 nickbeen/rick-and-morty-api-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 131
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-20