imdbphp/imdbphp

从IMDb检索电影和电视信息的库


README

PHP库,用于从IMDb检索电影和电视信息。可以检索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 (German, Germany)de (German)en (English)。如果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