kiwilan/php-tmdb

PHP wrapper package to interact with the The Movie Database (TMDB) API.

Fund package maintenance!
kiwilan

0.1.0 2024-09-18 16:24 UTC

This package is auto-updated.

Last update: 2024-09-18 16:30:00 UTC


README

Banner with a lots of movies and TV series in background and PHP TMDB title

php version downloads license tests codecov

PHP wrapper package to interact with the The Movie Database (TMDB) API.

Important

You need to create an account on TMDB and get an API key to use this package. It's free and easy to do, you can read this guide to get started.

Warning

This package is under development.

Requirements

PHP 8.1 and later.

Note

Package guzzlehttp/guzzle will be installed automatically by Composer.

About

This package uses repository pattern to interact with the TMDB API. Each repository represents an API category like Movies, Search, Trending, etc. And each endpoint of API is a method in repository, like details() for Movies, movie() for Search, all() for Trending, etc. If you know TMDB API, you will understand this package easily.

This is NOT official TMDB API PHP wrapper, you can check php-tmdb/api if you want official package.

Why this package?

All current PHP packages to interact with the TMDB API are not up-to-date and I need a modern and easy-to-use package to interact with the TMDB API. So I decided to create this package. You can check Roadmap to see what I plan to do with this package.

Installation

You can install the package via composer:

composer require kiwilan/php-tmdb

Usage of API

Collection

Collection: Details

Get collection details by ID.

use Kiwilan\Tmdb\Tmdb;

$collection = Tmdb::client('API_KEY')
    ->collections()
    ->details(collection_id: 119); // ?\Kiwilan\Tmdb\Models\TmdbCollection

Companies

Companies: Details

Get the company details by ID.

use Kiwilan\Tmdb\Tmdb;

$collection = Tmdb::client('API_KEY')
    ->companies()
    ->details(company_id: 12); // ?\Kiwilan\Tmdb\Models\TmdbCompany

Credits

Credits: Details

Get a movie or TV credit details by ID.

use Kiwilan\Tmdb\Tmdb;

$collection = Tmdb::client('API_KEY')
    ->credits()
    ->details(credit_id: '5256c8b219c2956ff6047cd8'); // ?\Kiwilan\Tmdb\Models\TmdbCredit

Movie Lists

Movie Lists: Now Playing

Get a list of movies that are currently in theatres.

use Kiwilan\Tmdb\Tmdb;

$now_playing = Tmdb::client('API_KEY')
    ->movieLists()
    ->nowPlaying(); // ?\Kiwilan\Tmdb\Results\MovieResults

Movie Lists: Popular

Get a list of movies ordered by popularity.

$popular = Tmdb::client('API_KEY')
    ->movieLists()
    ->popular(); // ?\Kiwilan\Tmdb\Results\MovieResults

Movie Lists: Top Rated

Get a list of movies ordered by rating.

$top_rated = Tmdb::client('API_KEY')
    ->movieLists()
    ->topRated(); // ?\Kiwilan\Tmdb\Results\MovieResults

Movie Lists: Upcoming

Get a list of movies that are being released soon.

$upcoming = Tmdb::client('API_KEY')
    ->movieLists()
    ->upcoming(); // ?\Kiwilan\Tmdb\Results\MovieResults

Movies

Movies: Details

Get the top level details of a movie by ID (you can use append_to_response option to get more details).

use Kiwilan\Tmdb\Tmdb;

$movie = Tmdb::client('API_KEY')
    ->movies()
    ->details(movie_id: 120); // ?\Kiwilan\Tmdb\Models\TmdbMovie

Networks

Networks: Details

Get the details of a network by ID.

use Kiwilan\Tmdb\Tmdb;

$network = Tmdb::client('API_KEY')
    ->networks()
    ->details(network_id: 49); // ?\Kiwilan\Tmdb\Models\TvSeries\TmdbNetwork

Search

Search: Collection

Search for collections by their original, translated and alternative names.

use Kiwilan\Tmdb\Tmdb;

$results = Tmdb::client('API_KEY')
    ->search()
    ->movie(query: 'the lord of the rings');

$collections = $results->getResults(); // \Kiwilan\Tmdb\Models\TmdbCollection[]
$firstCollection = $results->getFirstResult(); // ?\Kiwilan\Tmdb\Models\TmdbCollection

You can use options into your search:

use Kiwilan\Tmdb\Tmdb;
use Kiwilan\Tmdb\Query\SearchCollectionQuery;

$results = Tmdb::client('API_KEY')
    ->search()
    ->collection(query: 'le seigneur des anneaux', params: new SearchCollectionQuery(
        include_adult: true,
        language: 'fr-FR',
        page: 1,
        year: 2001,
    ));

Search: Movie

Search for movies by their original, translated and alternative titles.

use Kiwilan\Tmdb\Tmdb;

$results = Tmdb::client('API_KEY')
    ->search()
    ->movie(query:'the fellowship of the ring');

$movies = $results->getResults(); // \Kiwilan\Tmdb\Models\TmdbMovie[]
$firstMovie = $results->getFirstResult(); // ?\Kiwilan\Tmdb\Models\TmdbMovie

You can use options into your search:

use Kiwilan\Tmdb\Tmdb;
use Kiwilan\Tmdb\Query\SearchMovieQuery;

$results = Tmdb::client('API_KEY')
    ->search()
    ->movie(query: 'le seigneur des anneaux', params: new SearchMovieQuery(
        include_adult: true,
        language: 'fr-FR',
        primary_release_year: 2001,
        page: 1,
        region: 'en-US',
        year: 2001,
    ));

Search: TV

Search for TV shows by their original, translated and also known as names.

use Kiwilan\Tmdb\Tmdb;

$results = Tmdb::client('API_KEY')
    ->search()
    ->tv(query: 'game of thrones');

$tvSeries = $results->getResults(); // \Kiwilan\Tmdb\Models\TmdbTvSeries[]
$firstTvSeries = $results->getFirstResult(); // ?\Kiwilan\Tmdb\Models\TmdbTvSeries

You can use options into your search:

use Kiwilan\Tmdb\Tmdb;
use Kiwilan\Tmdb\Query\SearchTvSeriesQuery;

$results = Tmdb::client('API_KEY')
    ->search()
    ->tv(query: 'game of thrones', params: new SearchTvSeriesQuery(
        first_air_date_year: 2011,
        include_adult: true,
        language: 'fr-FR',
        page: 1,
        year: 2011,
    ));

Trending

Trending: All

Get the trending movies, TV shows and people.

use Kiwilan\Tmdb\Tmdb;

$trending = Tmdb::client('API_KEY')
    ->trending()
    ->all(); // ?\Kiwilan\Tmdb\Results\MediaResults

Trending: Movies

Get the trending movies on TMDB.

use Kiwilan\Tmdb\Tmdb;

$trending = Tmdb::client('API_KEY')
    ->trending()
    ->movies(); // ?\Kiwilan\Tmdb\Results\MovieResults

Trending: People

Get the trending people on TMDB.

use Kiwilan\Tmdb\Tmdb;

$trending = Tmdb::client('API_KEY')
    ->trending()
    ->people(); // ?\Kiwilan\Tmdb\Results\PeopleResults

Trending: TV

Get the trending TV shows on TMDB.

use Kiwilan\Tmdb\Tmdb;

$trending = Tmdb::client('API_KEY')
    ->trending()
    ->tv(); // ?\Kiwilan\Tmdb\Results\TvSerieResults

TV Series List

TV Series List: Airing Today

Get a list of TV shows airing today.

$all = Tmdb::client('API_KEY')
    ->tvSeriesList()
    ->airingToday(); // ?\Kiwilan\Tmdb\Results\TvSerieResults

TV Series List: On The Air

Get a list of TV shows that air in the next 7 days.

$all = Tmdb::client('API_KEY')
    ->tvSeriesList()
    ->onTheAir(); // ?\Kiwilan\Tmdb\Results\TvSerieResults

TV Series List: Popular

Get a list of TV shows ordered by popularity.

$all = Tmdb::client('API_KEY')
    ->tvSeriesList()
    ->popular(); // ?\Kiwilan\Tmdb\Results\TvSerieResults

TV Series List: Top Rated

Get a list of TV shows ordered by rating.

$all = Tmdb::client('API_KEY')
    ->tvSeriesList()
    ->topRated(); // ?\Kiwilan\Tmdb\Results\TvSerieResults

TV Series

TV Series: Details

Get the details of a TV show (you can use append_to_response option to get more details).

use Kiwilan\Tmdb\Tmdb;

$tvSeries = Tmdb::client('API_KEY')
    ->tvSeries()
    ->details(series_id: 1399); // ?\Kiwilan\Tmdb\Models\TmdbTvSeries

TV Seasons

TV Seasons: Details

Query the details of a TV season (you can use append_to_response option to get more details).

use Kiwilan\Tmdb\Tmdb;

$season = Tmdb::client('API_KEY')
    ->tvSeasons()
    ->details(series_id: 1399, season_number: 1); // ?\Kiwilan\Tmdb\Models\TmdbSeason

TV Episodes

TV Episodes: Details

Query the details of a TV episode (you can use append_to_response option to get more details).

use Kiwilan\Tmdb\Tmdb;

$episode = Tmdb::client('API_KEY')
    ->tvEpisodes()
    ->details(series_id: 1399, season_number: 1, episode_number: 1); // ?\Kiwilan\Tmdb\Models\TmdbEpisode

Images

For any model with image (poster, backdrop, logo, profile, still), you can use multiple methods:

use Kiwilan\Tmdb\Tmdb;

$movie = Tmdb::client('API_KEY')
    ->movies()
    ->details(movie_id: 120); // ?\Kiwilan\Tmdb\Models\TmdbMovie

$poster_path = $movie->getPosterPath(); // string|null (path to poster)
$poster_url = $movie->getPosterUrl(); // string|null (URL to poster)
$poster_image = $movie->getPosterImage(); // string|null (binary image)
$success = $movie->savePosterImage('path/to/save/poster.jpg'); // bool (true if success)

You can change the size of the image with size option, available for get*Url, get*Image and save*Image methods:

use Kiwilan\Tmdb\Tmdb;
use Kiwilan\Tmdb\Enums\PosterSize;

$movie = Tmdb::client('API_KEY')
    ->movies()
    ->details(movie_id: 120); // ?\Kiwilan\Tmdb\Models\TmdbMovie

$poster_url = $movie->getPosterUrl(size: PosterSize::W500); // string|null (URL to poster)

These methods are available for Poster, Backdrop, Logo, Profile and Still.

Append to response

TMDB offers an easy way to get more details with append_to_response option. You can add more data in same request, it's really useful to get all data you need in one request.

append_to_response is an easy and efficient way to append extra requests to any top level namespace. The movie, TV show, TV season, TV episode and person detail methods all support a query parameter called append_to_response. This makes it possible to make sub requests within the same namespace in a single HTTP request. Each request will get appended to the response as a new JSON object. From https://developer.themoviedb.org/docs/append-to-response

To know which methods support append_to_response, check if method has append_to_response parameter (always optional and at the end of parameters). And to know what you can add, check the official documentation.

Example with append_to_response:

use Kiwilan\Tmdb\Tmdb;

$movie = Tmdb::client('API_KEY')
    ->movies()
    ->details(movie_id: 120, append_to_response: ['credits' ,'videos']); // ?\Kiwilan\Tmdb\Models\TmdbMovie

Testing

composer test

Contributing

A fix? A new feature? A typo? You're welcome to contribute to this project. Just open a pull request.

Roadmap

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

https://github.com/kiwilan/php-tmdb