rooxie/omdb

OMDb API 的 PHP 封装器

1.0.0 2024-07-26 22:29 UTC

This package is auto-updated.

Last update: 2024-09-26 22:49:55 UTC


README

简介

使用面向对象方法和无依赖项的 OMDb API 的 PHP 封装器。

先决条件

  • PHP >= 7.1
  • PHP json 扩展
  • PHP curl 扩展

安装

使用 Composer 安装 omdb-php

composer require rooxie/omdb

快速入门

创建一个 OMDb 类的实例,将 API 密钥作为构造函数参数提供

$omdb = new Rooxie\OMDb('xxxxxxx');

通过 IMDb ID 获取标题

在通过 HTTP 请求获取电影数据后,可以得到 Movie 模型类的实例

$movie = $omdb->getByImdbId('tt0110912');

echo $movie->getTitle();        // Pulp Fiction
echo $movie->getReleased();     // 14 Oct 1994
echo $movie->getRuntime();      // 154
echo $movie->getImdbRating();   // 8.9
...

通过标题和其他可选参数获取

对于使用电影标题获取数据同样适用。还可以提供可选参数,例如电影类型(movieseriesepisode)和发行年份

$movie = $omdb->getByTitle('harry potter', 'movie', 2004);

echo $movie->getTitle();        // Harry Potter and the Prisoner of Azkaban
echo $movie->getImdbId();       // tt0304141
echo $movie->getRated();        // PG
echo $movie->getMetascore();    // 82
...

每个对象还有一个 $movie->toArray() 方法

Array
(
    [ImdbId] => tt0246578
    [Title] => Donnie Darko
    [Year] => 2001
    [Rated] => R
    [Released] => 26 Oct 2001
    [Runtime] => 113
    [Genre] => Array
        (
            [0] => Drama
            [1] => Sci-Fi
            [2] => Thriller
        )

    [Director] => Array
        (
            [0] => Richard Kelly
        )

    [Writer] => Array
        (
            [0] => Richard Kelly
        )

    [Actors] => Array
        (
            [0] => Jake Gyllenhaal
            [1] => Holmes Osborne
            [2] => Maggie Gyllenhaal
            [3] => Daveigh Chase
        )

    [Plot] => A troubled teenager is plagued by visions of a man in a large rabbit suit who manipulates him to commit a series of crimes, after he narrowly escapes a bizarre accident.
    [Language] => Array
        (
            [0] => English
        )

    [Country] => Array
        (
            [0] => USA
        )

    [Awards] => 11 wins & 15 nominations.
    [Poster] => https://m.media-amazon.com/images/M/MV5BZjZlZDlkYTktMmU1My00ZDBiLWFlNjEtYTBhNjVhOTM4ZjJjXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_.jpg
    [Type] => movie
    [DVD] => 19 Mar 2002
    [BoxOffice] => N/A
    [Production] => Newmarket Film Group
    [Website] => http://www.donniedarko.com
    [Metascore] => 88
    [RottenTomatoesRating] => 87
    [IMDbRating] => 8.1
    [IMDbVotes] => 695608
    [TotalSeasons] => N/A
    [SeriesID] => N/A
    [Season] => N/A
    [Episode] => N/A
)

通过标题和其他可选参数搜索

电影搜索方法返回 API 响应的原始数组,最后一个可选参数是分页参数

$movies = $omdb->search('arrival', 'movie', 2016, 1);

结果

Array
(
    [Search] => Array
        (
            [0] => Array
                (
                    [Title] => Arrival
                    [Year] => 2016
                    [imdbID] => tt2543164
                    [Type] => movie
                    [Poster] => https://m.media-amazon.com/images/M/MV5BMTExMzU0ODcxNDheQTJeQWpwZ15BbWU4MDE1OTI4MzAy._V1_SX300.jpg
                )

            [1] => Array
                (
                    [Title] => Alien Arrival
                    [Year] => 2016
                    [imdbID] => tt3013160
                    [Type] => movie
                    [Poster] => https://m.media-amazon.com/images/M/MV5BMjE5ODg2MTUtMDQ3Ny00MjA2LWJmZjMtYThlNTY3NGJhZmI2XkEyXkFqcGdeQXVyMjA4MDYxNDk@._V1_SX300.jpg
                )

            [2] => Array
                (
                    [Title] => The Arrival
                    [Year] => 2016
                    [imdbID] => tt5623378
                    [Type] => movie
                    [Poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BNTMyMDRhODgtOTE4Yi00MGI5LTllMDUtNThhYWM0ZGVhZTg1XkEyXkFqcGdeQXVyNjQ2NzI0Mw@@._V1_SX300.jpg
                )

            [3] => Array
                (
                    [Title] => Arrival
                    [Year] => 2016
                    [imdbID] => tt5433758
                    [Type] => movie
                    [Poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMTNiMDRlMDQtZTVkNS00MWVhLWFiOGUtMTkxNTgzODBkMWIyXkEyXkFqcGdeQXVyMjMxMTU4MzA@._V1_SX300.jpg
                )

            [4] => Array
                (
                    [Title] => The Arrival of the Train at a Subway Station in Vienna
                    [Year] => 2016
                    [imdbID] => tt5972896
                    [Type] => movie
                    [Poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMGVlY2MwYWUtODE1Zi00NGNjLWJjMDgtM2M0ZmJmYTFmMWM4XkEyXkFqcGdeQXVyNDcwNDE0Nzk@._V1_SX300.jpg
                )

            [5] => Array
                (
                    [Title] => Arrival: Common Ground
                    [Year] => 2016
                    [imdbID] => tt6196852
                    [Type] => movie
                    [Poster] => https://images-na.ssl-images-amazon.com/images/M/MV5BMTE4ZjA0MjctNmY0Yy00OWE2LTk1ZTItZmViZDg3ODQwY2NjL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyMzM2MjIzNA@@._V1_SX300.jpg
                )

            [6] => Array
                (
                    [Title] => The Arrival
                    [Year] => 2016
                    [imdbID] => tt5678770
                    [Type] => movie
                    [Poster] => N/A
                )

            [7] => Array
                (
                    [Title] => Dead on Arrival
                    [Year] => 2016
                    [imdbID] => tt5175144
                    [Type] => movie
                    [Poster] => N/A
                )

            [8] => Array
                (
                    [Title] => Arrival of a Train At
                    [Year] => 2016
                    [imdbID] => tt5333154
                    [Type] => movie
                    [Poster] => http://ia.media-imdb.com/images/M/MV5BY2M2YWYyMDUtNGM5MC00Y2YwLTkzZWEtNzgzOGEzYTQxYmQwXkEyXkFqcGdeQXVyNjQ3Mjc0Nzg@._V1_SX300.jpg
                )

            [9] => Array
                (
                    [Title] => Arrival of Their Majesties the Emperor and Empress of Japan
                    [Year] => 2016
                    [imdbID] => tt5378228
                    [Type] => movie
                    [Poster] => N/A
                )

        )

    [totalResults] => 10
    [Response] => True
)

错误处理

大多数基本错误都有相应的异常。

无效的 API 密钥

try {
    $movies = $omdb->getByTitle('a clockwork orange');
} catch (\Rooxie\Exception\InvalidApiKeyException $e) {
    echo $e->getMessage(); // Invalid API key "Invalid or missing API key"
}

电影未找到

try {
    $movies = $omdb->getByTitle('zaqxswcde');
} catch (\Rooxie\Exception\MovieNotFoundException $e) {
    echo $e->getMessage(); // Could not find movie "zaqxswcde"
}

IMDb ID 不正确

try {
    $movies = $omdb->getByImdbId('gj349gj349gj34');
} catch (\Rooxie\Exception\IncorrectImdbIdException $e) {
    echo $e->getMessage(); // Incorrect IMDb ID "gj349gj349gj34"
}

如果 API 返回格式错误的值,将抛出 InvalidResponseException

在其他所有情况下,将抛出 ApiErrorException