PHP code example of nickbeen / rick-and-morty-api-php

1. Go to this page and download the library: Download nickbeen/rick-and-morty-api-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nickbeen / rick-and-morty-api-php example snippets


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

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

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

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

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

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

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

echo $character->name;

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

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

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

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

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

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

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

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

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

echo $episode->name;

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

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

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

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

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

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

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

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

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

echo $location->name;

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

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

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

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