用于从IMDb获取电影和电视信息的库

v1.0.1 2022-03-23 03:53 UTC

This package is auto-updated.

Last update: 2024-09-28 10:25:30 UTC


README

用于从IMDb获取电影和电视信息的PHP库。可以获取到IMDb上几乎所有的信息,包括电影、电视剧、电视节目、人物。可以在IMDb上搜索标题,包括按类型(电影、电视剧等)筛选。可以下载电影海报和演员图片。

快速开始

$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();

安装

此库抓取imdb.com,因此它们网站的更改可能会使此库的部分功能失效。您可能每年需要更新几次。在选择安装/配置方式时请考虑这一点。

使用以下方法之一获取文件

要求

  • PHP >= 5.6
  • PHP cURL扩展

配置

imdbphp默认无需配置,但可以缓存IMDb搜索,存储图像并更改语言(如果配置)。

配置通过 src/Imdb/Config.php 中的 \Imdb\Config 类完成,该类详细说明了所有可用的配置选项。您可以通过创建对象,修改其属性然后将它传递给IMDb的构造函数来更改配置。

$config = new \Imdb\Config();
$config->language = 'de-DE,de,en';
$imdb = new \Imdb\Title(335266, $config);
$imdb->title(); // Lost in Translation - Zwischen den Welten
$imdb->orig_title(); // Lost in Translation

如果您使用git克隆,您可能更喜欢通过在conf文件夹中放置一个ini文件来配置IMDbPHP。900_localconf.sample 包含一些示例设置。

默认情况下,缓存文件夹是 ./cache。IMDb的请求将在此处缓存一周(默认),以加快未来请求的速度。

高级配置

替换默认缓存(磁盘缓存)

您可以通过将其传递给任何ImdbPHP类的构造函数来替换ImdbPHP使用的任何PSR-16(简单缓存)缓存。

将使用您的缓存与ImdbPHP配置的唯一部分是TTL,由 \Imdb\Config::$cache_expire 设置,默认为一周。

$cache = new \Cache\Adapter\PHPArray\ArrayCachePool();
// Search results will be cached
$search = new \Imdb\TitleSearch(null /* config */, null /* logger */, $cache);
$firstResultTitle = $search->search('The Matrix')[0];
// $firstResultTitle, an \Imdb\Title will also be using $cache for caching any page requests it does
$cache = new \Cache\Adapter\PHPArray\ArrayCachePool();
$title = new \Imdb\Title(335266, null /* config */, null /* logger */, $cache);

替换默认的记录器(它输出彩色HTML,默认禁用)

记录器主要会告诉你关于失败的错误级别的HTTP请求,每个HTTP请求的信息级别,以及一些缓存命中等调试信息。

$logger = new \Monolog\Logger('name');
$title = new \Imdb\Title(335266, null /* config */, $logger);

搜索电影

// include "bootstrap.php"; // Load the class in if you're not using an autoloader
$search = new \Imdb\TitleSearch(); // Optional $config parameter
$results = $search->search('The Matrix', array(\Imdb\TitleSearch::MOVIE)); // Optional second parameter restricts types returned

// $results is an array of Title objects
// The objects will have title, year and movietype available
// immediately, but any other data will have to be fetched from IMDb
foreach ($results as $result) { /* @var $result \Imdb\Title */
    echo $result->title() . ' ( ' . $result->year() . ')';
}

搜索人物

// include "bootstrap.php"; // Load the class in if you're not using an autoloader
$search = new \Imdb\PersonSearch(); // Optional $config parameter
$results = $search->search('Forest Whitaker');

// $results is an array of Person objects
// The objects will have name and imdbid available, everything else must be fetched from IMDb
foreach ($results as $result) { /* @var $result \Imdb\Person */
    echo $result->name();
}

演示网站

演示网站提供了一种快速确保一切正常的方法,一些示例代码,并让您轻松查看一些可用数据。

从此存储库根目录中的demo文件夹启动PHP的内置Web服务器,并浏览到 https://:8000

php -S localhost:8000

问题/帮助

SSL证书问题:无法获取本地颁发者证书

Windows

curl库可能没有与根SSL证书捆绑,或者它们已经过时。您需要设置它们。

  1. 下载 cacert.pem
  2. 将它存储在您的电脑上某个位置。
    C:\php\extras\ssl\cacert.pem
  3. 打开您的 php.ini 文件,在 [curl] 下添加以下内容
    curl.cainfo = "C:\php\extras\ssl\cacert.pem"
  4. 重启您的 web 服务器。

Linux

cURL 默认使用 Linux 中的证书授权文件,但该文件可能已经过时。请查找您操作系统的说明以更新 CA 文件或更新您的发行版。

配置语言

有时 IMDb 会对指定的语言是否正确产生怀疑,如果您只指定唯一的语言和地区代码(如 de-DE)。以下示例中,我们可以看到我们选择了包含 de-DE (德国,德国)de (德语)en (英语)。如果 IMDb 找不到与德国,德国相匹配的内容,您将得到德语结果;如果没有德语翻译,则得到英语结果。

$config = new \Imdb\Config();
$config->language = 'de-DE,de,en';
$imdb = new \Imdb\Title(335266, $config);
$imdb->title(); // Lost in Translation - Zwischen den Welten
$imdb->orig_title(); // Lost in Translation

请使用 Unicode 联盟的 语言-地区信息 数据库来查找您的唯一语言和地区代码。

找到您的唯一语言和地区代码后,您需要将它们组合起来。从语言代码(如 de)开始,添加分隔符(-),最后是地区代码(DE);de-DE。现在包括您的语言代码(de);de-DE,de。最后一步是添加英语(en);de-DE,de,en