aharen / omdbapi
Brian Fritz 开发的用于 OMDbAPI.com 的 PHP 包
v2.1.3
2023-12-12 13:10 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9
README
Brian Fritz 开发的用于与 OMDbAPI.com API 通信的 PHP 类
如何使用
包含文件
composer require aharen/omdbapi
或者更新你的 composer.json 文件
require {
"aharen/omdbapi" : "2.0.*"
}
初始化
由于 API 将变为私有 (了解详情) 并需要 API 密钥,因此对 API 的初始化方式和如何使用海报 API 进行了一些小的更改。
use aharen\OMDbAPI(); $omdb = new OMDbAPI($api_key, $image_host, $assoc);
第一个参数将是 API 密钥,第二个是使用的主机(false = omdbapi,true = poster api,默认为 false)& 第三个是 assoc(true = assoc on,false = assoc off,默认为 false)
搜索 OMDb API
需要搜索关键词,接受类型(电影、电视剧或剧集)& 年份
use aharen\OMDbAPI; $omdb = new OMDbAPI(); $omdb->search($keyword, $type, $year);
示例用法
$omdb->search('spider');
输出
stdClass Object ( [code] => 200 [message] => OK [data] => stdClass Object ( [0] => stdClass Object ( [Title] => Spider-Man [Year] => 2002 [imdbID] => tt0145487 [Type] => movie ) [1] => stdClass Object ( [Title] => The Amazing Spider-Man [Year] => 2012 [imdbID] => tt0948470 [Type] => movie ) [2] => stdClass Object ( [Title] => Spider-Man 2 [Year] => 2004 [imdbID] => tt0316654 [Type] => movie ) [3] => stdClass Object ( [Title] => Spider-Man 3 [Year] => 2007 [imdbID] => tt0413300 [Type] => movie ) [4] => stdClass Object ( [Title] => The Amazing Spider-Man 2 [Year] => 2014 [imdbID] => tt1872181 [Type] => movie ) [5] => stdClass Object ( [Title] => Along Came a Spider [Year] => 2001 [imdbID] => tt0164334 [Type] => movie ) [6] => stdClass Object ( [Title] => Spider [Year] => 2002 [imdbID] => tt0278731 [Type] => movie ) [7] => stdClass Object ( [Title] => Spider-Man [Year] => 1994–1998 [imdbID] => tt0112175 [Type] => series ) [8] => stdClass Object ( [Title] => Kiss of the Spider Woman [Year] => 1985 [imdbID] => tt0089424 [Type] => movie ) [9] => stdClass Object ( [Title] => The Spectacular Spider-Man [Year] => 2008–2009 [imdbID] => tt0976192 [Type] => series ) ) )
使用示例
// search for all 'series' that contain 'spider' in the title $omdb->search('spider', 'series'); // search for all 'series' that contain 'spider' in the title and is from '2014' $omdb->search('spider', 'series', '2014');
关联模式
您也可以使用此库在关联模式下,结果为数组而不是 stdClass 实例,将第二个构造函数参数传递为 true
// Associative mode (results will be associative arrays)
$omdb = new OMDbAPI(null, true);
获取电影详情
获取电影、电视剧或剧集的详情。可以通过 IMDB ID 或标题获取详情
使用示例
// get details for IMDB ID 'tt0338013' $omdb->fetch('i', 'tt0338013'); // get details for title 'eternal sunshine' $omdb->fetch('t', 'eternal sunshine');
上述查询的输出结果
stdClass Object ( [code] => 200 [message] => OK [data] => stdClass Object ( [Title] => Eternal Sunshine of the Spotless Mind [Year] => 2004 [Rated] => R [Released] => 19 Mar 2004 [Runtime] => 108 min [Genre] => Drama, Romance, Sci-Fi [Director] => Michel Gondry [Writer] => Charlie Kaufman (story), Michel Gondry (story), Pierre Bismuth (story), Charlie Kaufman (screenplay) [Actors] => Jim Carrey, Kate Winslet, Gerry Robert Byrne, Elijah Wood [Plot] => When their relationship turns sour, a couple undergoes a procedure to have each other erased from their memories. But it is only through the process of loss that they discover what they had to begin with. [Language] => English [Country] => USA [Awards] => Won 1 Oscar. Another 64 wins & 62 nominations. [Poster] => http://ia.media-imdb.com/images/M/MV5BMTY4NzcwODg3Nl5BMl5BanBnXkFtZTcwNTEwOTMyMw@@._V1_SX300.jpg [Metascore] => 89 [imdbRating] => 8.4 [imdbVotes] => 533,088 [imdbID] => tt0338013 [Type] => movie [Response] => True ) )
获取剧集详情
您也可以使用 fetch
的第三个参数添加额外参数,例如
// Dexter (TV show) $omdb->fetch('i', 'tt0773262', ['Season' => 1])
这将添加 Season=1
参数
致谢
单元测试与关联模式由 @Gregwar 提供(干杯)