PHP code example of lukaszzychal / tmdb-client-php

1. Go to this page and download the library: Download lukaszzychal/tmdb-client-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/ */

    

lukaszzychal / tmdb-client-php example snippets




use LukaszZychal\TMDB\Client\TMDBClient;
use LukaszZychal\TMDB\Utils\LicenseCompliance;

// Load API key from .env file or environment variable
$apiKey = $_ENV['TMDB_API_KEY'] ?? getenv('TMDB_API_KEY');

// Initialize the client
$client = new TMDBClient($apiKey);

// Get popular movies
$popularMovies = $client->movies()->getPopular();
$movies = json_decode($popularMovies->getBody()->getContents(), true);

// Display movies with TMDB attribution (REQUIRED for compliance)
foreach ($movies['results'] as $movie) {
    echo "<h3>{$movie['title']}</h3>";
    echo "<p>{$movie['overview']}</p>";
    
    // MANDATORY: Include TMDB attribution
    echo LicenseCompliance::generateHtmlAttribution();
}

// Get movie details
$movieDetails = $client->movies()->getDetails(550); // Fight Club
$movie = json_decode($movieDetails->getBody()->getContents(), true);

// Search for movies
$searchResults = $client->search()->movies('Inception');
$results = json_decode($searchResults->getBody()->getContents(), true);


use LukaszZychal\TMDB\Utils\LicenseCompliance;

// Generate attribution HTML
$attribution = LicenseCompliance::generateHtmlAttribution();

// Validate your HTML content
$validation = LicenseCompliance::validateHtmlAttribution($yourHtmlContent);

// Check compliance status
$compliance = LicenseCompliance::checkCompliance([
    'requests_per_day' => 500,
    'has_attribution' => true,
    'has_logo' => true
]);

if (!$compliance['is_compliant']) {
    echo "Issues: " . implode(', ', $compliance['issues']);
}

$movies = $client->movies();

// Get movie details
$movie = $movies->getDetails($movieId, ['language' => 'en-US']);

// Get popular movies
$popular = $movies->getPopular(['page' => 1]);

// Get now playing movies
$nowPlaying = $movies->getNowPlaying(['page' => 1, 'region' => 'US']);

// Get upcoming movies
$upcoming = $movies->getUpcoming(['page' => 1]);

// Get top rated movies
$topRated = $movies->getTopRated(['page' => 1]);

// Get movie credits
$credits = $movies->getCredits($movieId);

// Get movie reviews
$reviews = $movies->getReviews($movieId, ['page' => 1]);

// Get movie videos
$videos = $movies->getVideos($movieId, ['language' => 'en-US']);

// Get movie images
$images = $movies->getImages($movieId, ['language' => 'en-US']);

// Get similar movies
$similar = $movies->getSimilar($movieId, ['page' => 1]);

// Get recommendations
$recommendations = $movies->getRecommendations($movieId, ['page' => 1]);

$tv = $client->tv();

// Get TV show details
$show = $tv->getDetails($tvId, ['language' => 'en-US']);

// Get popular TV shows
$popular = $tv->getPopular(['page' => 1]);

// Get TV shows airing today
$airingToday = $tv->getAiringToday(['page' => 1]);

// Get TV shows on the air
$onTheAir = $tv->getOnTheAir(['page' => 1]);

// Get top rated TV shows
$topRated = $tv->getTopRated(['page' => 1]);

// Get season details
$season = $tv->getSeasonDetails($tvId, $seasonNumber);

// Get episode details
$episode = $tv->getEpisodeDetails($tvId, $seasonNumber, $episodeNumber);

$people = $client->people();

// Get person details
$person = $people->getDetails($personId, ['language' => 'en-US']);

// Get popular people
$popular = $people->getPopular(['page' => 1]);

// Get person movie credits
$movieCredits = $people->getMovieCredits($personId);

// Get person TV credits
$tvCredits = $people->getTVCredits($personId);

// Get person combined credits
$combinedCredits = $people->getCombinedCredits($personId);

$search = $client->search();

// Search movies
$movies = $search->movies('Inception', ['year' => 2010]);

// Search TV shows
$tvShows = $search->tv('Breaking Bad', ['first_air_date_year' => 2008]);

// Search people
$people = $search->people('Leonardo DiCaprio');

// Multi search (movies, TV shows, people)
$multi = $search->multi('Marvel');

// Get API configuration
$config = $client->configuration()->getDetails();

// Get movie genres
$movieGenres = $client->genres()->getMovieList(['language' => 'en-US']);

// Get TV genres
$tvGenres = $client->genres()->getTVList(['language' => 'en-US']);

use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Create a logger
$logger = new Logger('tmdb');
$logger->pushHandler(new StreamHandler('tmdb.log', Logger::INFO));

// Initialize client with custom configuration
$client = new TMDBClient(
    'your-api-key',
    [
        'timeout' => 60,
        'connect_timeout' => 15,
        'headers' => [
            'User-Agent' => 'MyApp/1.0'
        ]
    ],
    $logger
);

use LukaszZychal\TMDB\Exception\AuthenticationException;
use LukaszZychal\TMDB\Exception\NotFoundException;
use LukaszZychal\TMDB\Exception\RateLimitException;
use LukaszZychal\TMDB\Exception\TMDBException;

try {
    $movie = $client->movies()->getDetails(999999999);
} catch (AuthenticationException $e) {
    // Invalid API key
    echo "Authentication failed: " . $e->getMessage();
} catch (NotFoundException $e) {
    // Resource not found
    echo "Movie not found: " . $e->getMessage();
} catch (RateLimitException $e) {
    // Rate limit exceeded
    echo "Rate limit exceeded: " . $e->getMessage();
} catch (TMDBException $e) {
    // Other TMDB API errors
    echo "API Error: " . $e->getMessage();
}
bash
composer 
bash
   export TMDB_API_KEY=your-api-key-here
   
bash
php examples/basic-usage.php
php examples/license-compliance-example.php