imdbphp

repository·master·Indexed 42 days ago

https://github.com/tboothman/imdbphp

A PHP library for scraping and retrieving film and TV metadata from IMDb, including titles, series, episodes, and people. It features a disk-based cache, GraphQL query support, and specialized classes for retrieving person biographies, filmographies, and box-office charts.

Tokens
7.7K
Snippets
29
Records
39
Agent score
54%

What's inside imdbphp

  1. Replace the default disk cache with PSR-16

    master

    By default, imdbphp uses a disk cache in the ./cache folder. You can replace this with any PSR-16 (simple cache) implementation by passing the cache object to the constructor of any imdbphp class.

    Note that only the \Imdb\Config::$cache_expire property (which defaults to 1 week) will be used by your custom cache.

    $cache = new \Cache\Adapter\PHPArray\ArrayCachePool();
    
    // Pass to TitleSearch
    $search = new \Imdb\TitleSearch(null /* config */, null /* logger */, $cache);
    $firstResultTitle = $search->search('The Matrix')[0];
    
    // Pass to Title
    $title = new \Imdb\Title(335266, null /* config */, null /* logger */, $cache);
  2. Replace the default logger

    master

    The default logger echos colored HTML and is disabled by default. You can provide your own logger (such as a Monolog instance) by passing it as the third argument to the constructor of imdbphp classes. The logger handles error levels for failed HTTP requests, info for requests, and debug for cache hits.

    $logger = new \Monolog\Logger('name');
    $title = new \Imdb\Title(335266, null /* config */, $logger);
  3. Quick Start: Retrieve film and person data

    master

    To get started, include the library via Composer or include bootstrap.php if you are not using an autoloader. You can then instantiate \Imdb\Title to get film metadata or \Imdb\Person to get information about individuals.

    Example of retrieving film ratings and plot outlines, and then finding information about the director:

    $title = new \Imdb\Title(335266);
    $rating = $title->rating();
    $plotOutline = $title->plotoutline();
    
    # Find out about the director
    $person = new \Imdb\Person($title->director()[0]['imdb']);
    $name = $person->name();
    $photo = $person->photo();
  4. Install imdbphp

    master

    You can install imdbphp using one of the following methods:

    1. Composer (Recommended): Include the imdbphp/imdbphp package.
    2. Git Clone: Clone the repository and checkout the latest release tag.
    3. Zip/Tar Download: Download the latest release from the GitHub releases page.

    Note: This library scrapes imdb.com. Changes to the IMDb website may cause the library to fail, so you should plan to update it periodically.

    composer require imdbphp/imdbphp
  5. Configure language settings

    master

    You can configure the language used for IMDb lookups using the \Imdb\Config class. To ensure the best results, it is recommended to provide a comma-separated list of language-territory codes, starting with the most specific, followed by the general language, and finally a fallback like English.

    Use the Unicode Consortium Language-Territory Information database to find correct codes (e.g., de-DE for German/Germany).

    $config = new \Imdb\Config();
    $config->language = 'de-DE,de,en';
    $imdb = new \Imdb\Title(335266, $config);
    $imdb->title(); // Returns localized title
    $imdb->orig_title(); // Returns original title
  6. Configure imdbphp via INI files

    master

    You can override the default configuration settings by providing an .ini file. When initializing the Imdb\Config class, you can pass an optional path to a specific configuration file. If no path is provided, the class will automatically attempt to load all .ini files found in the conf/ directory (relative to the library root).

    Settings in the .ini file are mapped directly to the public properties of the Config class.

    use Imdb\Config;
    
    // Load a specific configuration file
    $config = new Config('/path/to/your/custom_config.ini');
    
    // Or let it load defaults from the 'conf/' directory automatically
    $config = new Config();
  7. Troubleshoot SSL certificate problems

    master

    If you encounter the error SSL certificate problem: unable to get local issuer certificate, follow these steps based on your OS:

    Windows

    1. Download cacert.pem.
    2. Store it in a permanent location (e.g., C:\php\extras\ssl\cacert.pem).
    3. Edit your php.ini and add/update the following under the [curl] section: curl.cainfo = "C:\php\extras\ssl\cacert.pem"
    4. Restart your webserver.

    Linux

    Update your system's CA certificate authority file using your distribution's package manager (e.g., apt or yum).

  8. Search for people

    master

    Use \Imdb\PersonSearch to find people. The search returns an array of \Imdb\Person objects.

    Note: The returned \Imdb\Person objects will have name() and imdbid() available immediately, but other data must be fetched from IMDb.

    $search = new \Imdb\PersonSearch();
    $results = $search->search('Forest Whitaker');
    
    foreach ($results as $result) {
        /* @var $result \Imdb\Person */
        echo $result->name();
    }
  9. Search for films

    master

    Use \Imdb\TitleSearch to find films. You can optionally restrict the search results to specific types (e.g., \Imdb\TitleSearch::MOVIE).

    Note: The returned \Imdb\Title objects will have title(), year(), and movietype() available immediately, but other metadata requires an additional fetch from IMDb.

    $search = new \Imdb\TitleSearch();
    $results = $search->search('The Matrix', array(\Imdb\TitleSearch::MOVIE));
    
    foreach ($results as $result) {
        /* @var $result \Imdb\Title */
        echo $result->title() . ' ( ' . $result->year() . ')';
    }
  10. Configure GraphQL error handling via throwHttpExceptions

    master

    The Imdb\GraphQL client's behavior during failed HTTP requests is determined by the throwHttpExceptions setting in your Config object.

    • If throwHttpExceptions is true: The client will throw an Imdb\Exception\Http exception containing the HTTP status code if the request fails (non-200 status).
    • If throwHttpExceptions is false: The client will log the error and return an empty stdClass object instead of throwing an exception.
  11. Configure the disk-based cache

    master

    The Imdb\Cache implementation stores scraped data as files on disk. To control its behavior, you must configure the following keys in your Imdb\Config object:

    • cachedir: The directory path where cache files are stored. The class will attempt to create this directory if it doesn't exist.
    • usecache: Boolean. If true, the cache will be checked for existing data during get() calls.
    • storecache: Boolean. If true, data will be written to disk during set() calls.
    • cache_expire: Integer (seconds). Determines the age at which cache files are considered expired and removed during a purge().
    • usezip: Boolean. If true, cache files are compressed using Gzip.
    • converttozip: Boolean. If true and usezip is enabled, the cache will automatically convert uncompressed files to Gzip format when they are accessed via get().