Jikan PHP API

repository·master·Indexed 22 days ago

https://github.com/jikan-me/jikan

An unofficial PHP API for MyAnimeList.net that scrapes the website to provide anime and manga data functionality. It includes a PHP wrapper (v4) compatible with PHP ^8.1 and Laravel/Lumen ^9 or ^10, as well as a standalone REST API service. The library provides data models for Anime, Manga, Person, Club, and Producer entities, allowing developers to access metadata such as titles, scores, genres, and rankings without building custom parsers.

Tokens
6K
Snippets
15
Records
25
Agent score
76%

What's inside Jikan

  1. Install Jikan PHP API via Composer

    master

    To use Jikan in your PHP project, install the latest version (v4) using Composer.

    Requirements:

    • PHP ^8.1
    • Compatible with Lumen/Laravel ^9 or ^10

    Note: Jikan does not support authenticated requests; you cannot use it to update MyAnimeList user lists.

    composer require jikan-me/jikan ^4
  2. Run tests using PHPUnit or GrumPHP

    master

    If you are contributing to the project or running it in a CI environment, you can execute tests using the following commands:

    PHPUnit: Runs the standard PHPUnit test suite.

    GrumPHP: Runs a combination of PHPCS, PHPLint, and PHPUnit.

    # Run PHPUnit
    php vendor/bin/phpunit
    
    # Run GrumPHP
    php vendor/bin/grumphp run
  3. Access Manga metadata and properties

    master

    The Manga class provides several getter methods to retrieve information about a manga entity.

    Core Identity & Links:

    • getMalId(): Returns the MyAnimeList ID (int).
    • getUrl(): Returns the MyAnimeList URL (string).
    • getTitles(): Returns an array of \Jikan\Model\Common\Title objects. Note: getTitle(), getTitleEnglish(), getTitleJapanese(), and getTitleSynonyms() are deprecated in favor of this method.

    Content & Status:

    • getSynopsis(): Returns the manga synopsis (string|null).
    • getBackground(): Returns background information (string|null).
    • getStatus(): Returns the current status (e.g., 'Publishing') (string|null).
    • isPublishing(): Returns true if the status is 'Publishing' (bool).
    • getPublished(): Returns a DateRange object (DateRange).
    • getVolumes(): Returns the number of volumes (int|null).
    • getChapters(): Returns the number of chapters (int|null).
    • getType(): Returns the manga type (string|null).

    Media & Classification:

    • getImages(): Returns a CommonImageResource object.
    • getGenres(): Returns an array of MalUrl objects.
    • getExplicitGenres(): Returns an array of MalUrl objects.
    • getDemographics(): Returns an array of MalUrl objects.
    • getThemes(): Returns an array of MalUrl objects.
    • getAuthors(): Returns an array of MalUrl objects.
    • getSerializations(): Returns an array of MalUrl objects.
    • getRelated(): Returns an array of MalUrl objects.
    • getExternalLinks(): Returns an array of Url objects.

    Statistics & Ranking:

    • getScore(): Returns the score (float|null).
    • getScoredBy(): Returns the number of users who scored it (int|null).
    • getRank(): Returns the rank (int|null).
    • getPopularity(): Returns the popularity rank (int|null).
    • getMembers(): Returns the number of members (int|null).
    • getFavorites(): Returns the number of favorites (int|null).
  4. Parse an AnimeCard using parseAnimeCard()

    master

    The AnimeCard class represents a summary of an anime, typically used in lists. You can instantiate an AnimeCard by calling the static parseAnimeCard() method and passing an instance of Jikan\Parser\Common\AnimeCardParser. This method populates the model with data such as the MyAnimeList ID, title, images, synopsis, genres, and score.

    use Jikan\Model\Common\AnimeCard;
    use Jikan\Parser\Common\AnimeCardParser;
    
    // Assuming $parser is an existing instance of AnimeCardParser
    $animeCard = AnimeCard::parseAnimeCard($parser);
  5. Instantiate a Person using fromParser()

    master

    To create a Person instance from parsed data, use the static fromParser method. This method requires an instance of Jikan\Parser\Person\PersonParser and populates all properties of the Person object from the parser's output.

    use Jikan\Model\Person\Person;
    use Jikan\Parser\Person\PersonParser;
    
    // Assuming $parser is an existing instance of PersonParser
    $person = Person::fromParser($parser);
    
    echo $person->getName();
    echo $person->getMalId();
  6. Access Club data with the Club model

    master

    The Jikan\Model\Club\Club class represents a MyAnimeList club entity. You can retrieve various attributes of a club, such as its ID, name, member count, and related resources (anime, manga, characters, etc.).

    /** @var Club $club */
    $id = $club->getMalId();
    $name = $club->getName();
    $members = $club->getMembers();
    $url = $club->getUrl();
    $category = $club->getCategory();
    $created = $club->getCreated(); // Returns \DateTimeImmutable
    $access = $club->getAccess();
    $images = $club->getImages(); // Returns ClubImageResource
    
    // Relations return arrays of Jikan\Model\Common\MalUrl
    $staff = $club->getStaff();
    $anime = $club->getAnime();
    $manga = $club->getManga();
    $characters = $club->getCharacters();
  7. Access data from an AnimeSearchListItem

    master

    The AnimeSearchListItem class represents an individual anime entry returned from a search result. You can retrieve metadata about the anime using its getter methods.

    Available properties and methods:

    • getMalId(): Returns the MyAnimeList ID (int).
    • getUrl(): Returns the MyAnimeList URL (string).
    • getImages(): Returns a CommonImageResource object containing image data.
    • getTitle(): Returns the anime title (string).
    • getSynopsis(): Returns the anime synopsis (string).
    • getType(): Returns the anime type (e.g., TV, Movie) (string).
    • getEpisodes(): Returns the number of episodes (int).
    • getScore(): Returns the anime score (float).
    • getStartDate(): Returns the start date as a \DateTimeImmutable or null.
    • getEndDate(): Returns the end date as a \DateTimeImmutable or null.
    • getMembers(): Returns the number of members (int).
    • getRated(): Returns the rating (e.g., PG-13) or null (string).
    • isAiring(): Returns true if the anime is currently airing (bool).
    /** @var \Jikan\Model\Search\AnimeSearchListItem $item */
    $title = $item->getTitle();
    $malId = $item->getMalId();
    $isAiring = $item->isAiring();
    $score = $item->getScore();
  8. Instantiate AnimeListItem using factory()

    master

    The AnimeListItem class represents an anime entry within a user's MyAnimeList. It is instantiated using the static factory() method, which accepts a stdClass object containing the raw data from the API. The factory method handles the parsing of dates, image resources, and the construction of related objects like MalUrl for genres, studios, and licensors.

    $item = new stdClass();
    // ... populate $item with properties matching the API response (e.g., anime_id, anime_title, etc.)
    
    $animeListItem = \Jikan\Model\User\AnimeListItem::factory($item);
  9. Use the Producer model

    master

    The Producer class represents a MyAnimeList producer entity and includes paginated results. It provides access to producer details such as their MAL ID, name, titles, images, establishment date, favorites count, biography, and external links. Because it implements Results and Pagination, it can also be used to iterate through collections of producers and check for subsequent pages.

    Note: The getName() method is deprecated; use getTitles() to access the full list of titles instead.

    use Jikan\Model\Producer\Producer;
    use Jikan\Parser\Producer\ProducerParser;
    
    // Assuming $parser is an instance of ProducerParser provided by the library
    $producer = Producer::fromParser($parser);
    
    echo $producer->getMalId();
    echo $producer->getAbout();
    
    if ($producer->hasNextPage()) {
        echo "More results available.";
    }
    
    foreach ($producer->getResults() as $result) {
        // Process individual producer results
    }
  10. Instantiate an Anime model from a parser

    master

    You can create an instance of the Anime class using the static fromParser() method, which accepts an AnimeParser instance. This method populates the model with all available data from the parser.

    $anime = Jikan\Model\Anime\Anime::fromParser($animeParser);
  11. Instantiate an AnimeSearchListItem from a parser

    master

    To create an instance of AnimeSearchListItem, use the static fromParser method. This method takes a \Jikan\Parser\Search\AnimeSearchListItemParser instance and populates the model with the parsed data.

    Note: This is typically used internally by the library's search functionality, but it is the primary way to transform parsed search data into this model.

    use Jikan\Model\Search\AnimeSearchListItem;
    
    $animeItem = AnimeSearchListItem::fromParser($parser);