php-tmdb/api
PHP client for The Movie Database (TMDb) API. Search movies, TV shows, people, and collections, fetch details, images, credits, and more via a clean object-oriented wrapper. Useful for Laravel or any PHP app needing TMDb data.
Install via Composer
composer require php-tmdb/api
Publish Config (Optional but Recommended)
php artisan vendor:publish --provider="Tmdb\TmdbServiceProvider" --tag="config"
Edit config/tmdb.php to include your API key and preferred HTTP client (e.g., GuzzleHttp\Client).
First Use Case: Fetch a Movie
use Tmdb\Tmdb;
$tmdb = new Tmdb(config('tmdb.api_key'));
$movie = $tmdb->getMovie(550); // Fetch *Fight Club*
dd($movie->title, $movie->overview, $movie->poster_path);
Key Configuration
tmdb.api_key in .env or config/tmdb.php.tmdb.client = GuzzleHttp\Client).$tmdb->getMovie($id), $tmdb->getMoviesNowPlaying()$tmdb->getTvShow($id), $tmdb->getTvShowsOnTheAir()$tmdb->getPerson($id), $tmdb->getPersonCredits($id)$tmdb->searchMovie($query), $tmdb->searchMulti($query)Example: Fetch Trending Movies
$trending = $tmdb->getTrending('movie', ['time_window' => 'day']);
foreach ($trending->results as $movie) {
echo $movie->title . " (" . $movie->release_date->format('Y') . ")\n";
}
Use getMovies() with query params:
$popularMovies = $tmdb->getMovies('popular', [
'page' => 2,
'region' => 'US',
'sort_by' => 'vote_average.desc'
]);
Access images via poster_path, backdrop_path, etc., and construct URLs:
$posterUrl = $tmdb->imageUrl($movie->poster_path, 'w500');
// Outputs: https://image.tmdb.org/t/p/w500/abc123.jpg
Service Provider Binding (Optional)
Bind the client in AppServiceProvider:
$this->app->singleton(Tmdb::class, function ($app) {
return new Tmdb(config('tmdb.api_key'), $app->make('http.client'));
});
Now inject Tmdb into controllers/services:
public function showMovie(Tmdb $tmdb, $id) {
$movie = $tmdb->getMovie($id);
return view('movie', compact('movie'));
}
Cache API responses for 24 hours (adjust as needed):
$movie = Cache::remember("tmdb_movie_{$id}", now()->addHours(24), function () use ($tmdb, $id) {
return $tmdb->getMovie($id);
});
API Key Leaks
config/tmdb.php or .env to version control..env for sensitive keys.Rate Limiting
429 Too Many Requests gracefully:
try {
$tmdb->getMovie($id);
} catch (\Tmdb\Exceptions\RateLimitException $e) {
// Retry after delay or notify admin
}
Deprecated Endpoints
getMovieChanges()) exist in TMDb’s API docs.getMovieVideos() may require manual path construction if the wrapper is outdated.Image Paths
poster_path/backdrop_path can be null. Always check:
$posterUrl = $movie->poster_path ? $tmdb->imageUrl($movie->poster_path) : null;
Enable Guzzle Debugging
Add to config/tmdb.php:
'options' => [
'debug' => env('TMDB_DEBUG', false),
],
Check logs for raw API responses during issues.
Validate Responses
Use dd($tmdb->getMovie($id)->toArray()) to inspect raw data structure.
Custom HTTP Client Extend the client for middleware (e.g., logging, retry logic):
$client = new \GuzzleHttp\Client([
'timeout' => 10,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]);
$tmdb = new Tmdb($apiKey, $client);
Add New Endpoints
If missing, extend the Tmdb class or create a decorator:
class ExtendedTmdb extends Tmdb {
public function getCustomEndpoint($path, $params = []) {
return $this->request('get', $path, $params);
}
}
Laravel Eloquent Models Sync TMDb data with local models:
// Example: Sync movie metadata to a `Movie` model
$tmdbMovie = $tmdb->getMovie($id);
Movie::updateOrCreate(
['tmdb_id' => $id],
[
'title' => $tmdbMovie->title,
'overview' => $tmdbMovie->overview,
'poster_path' => $tmdbMovie->poster_path,
]
);
searchMulti() for combined searches (movies, TV, people):
$results = $tmdb->searchMulti('Inception', ['page' => 1]);
@if($movie->poster_path)
<img src="{{ $tmdb->imageUrl($movie->poster_path, 'w300') }}">
@endif
How can I help you explore Laravel packages today?