letsplaybar / tvdb
Laravel 的 TVDB API 封装。
1.0.0
2020-03-20 09:56 UTC
Requires
- php: ^7.2
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2024-09-08 10:21:44 UTC
README
letsplaybar/tvdb
包提供易于使用的函数,帮助您与 TVDB API 交互。
安装
您可以通过 composer 安装此包
composer require letsplaybar/tvdb
使用以下 artisan 命令发布配置文件
php artisan vendor:publish --provider="Letsplaybar\TVDB\TVDBServiceProvider"
配置
上面的发布命令会将一个 tvdb.php
配置文件发布到您的 Laravel 配置文件夹中。请确保使用您个人的 API 详细信息调整这些值。
我建议不要触摸配置文件,而是在您项目的 .env
文件中定义您的 API 详细信息,如下所示
TVDB_API_KEY=
TVDB_USER_KEY=
TVDB_USERNAME=
TVDB_LANGUAGE=
您可以在 thetvdb.com 获取这些数据,语言值可以在以下位置找到: https://api.thetvdb.com/languages 在缩写属性中。别忘了清除配置缓存。(php artisan config:cache
)
使用方法
通过 ID 查找剧集
getSeries
函数返回一个 Series
对象。
// Find a series by its TVDB ID
// ID: 73739 (Lost)
$result = TVDB::getSeries(73739);
echo $result->title; // "Lost"
搜索剧集
search
函数返回一个 Series
对象数组,如果没有找到结果,则返回空数组。
// Search by title
$results = TVDB::search('Planet Earth');
// Search by IMDB ID
$results = TVDB::search(['imdbId' => 'tt5491994']);
// Search by zap2it ID
$results = TVDB::search(['zap2itId' => 'SH303483']);
获取剧集图片
为了获取一个剧集图片的数组,您需要指定您想要检索的图片类型。以下列出了可用的类型。
/*
* Get the images of the series by TVDB ID
* ID: 73739 (Lost)
*
* Available image types:
* - TVDB::IMAGE_TYPE_FANART
* - TVDB::IMAGE_TYPE_POSTER
* - TVDB::IMAGE_TYPE_SEASON
* - TVDB::IMAGE_TYPE_SERIES
*/
$images = TVDB::getSeriesImages(73739, TVDB::IMAGE_TYPE_POSTER);
// Or get the images directly from a "Series" object
$series = TVDB::getSeries(73739);
$images = $series->getImages(TVDB::IMAGE_TYPE_FANART);
获取剧集演员
以下选项可用于获取演员数组。
/*
* Get the actors of the series by TVDB ID
* ID: 73739 (Lost)
*/
$actors = TVDB::getSeriesActors(73739);
// Or get the actors directly from a "Series" object
$series = TVDB::getSeries(73739);
$actors = $series->getActors();
获取剧集集数
TVDB API 端点用于检索集数是分页的。这意味着在检索集数时,您需要指定一个页面号。
/*
* Get the episodes of the series by TVDB ID
* ID: 73739 (Lost)
*
* The second parameter specifies the page (page 1 by default)
*/
$episodes = TVDB::getSeriesEpisodes(73739, 2);
// Or get the episodes directly from a "Series" object
$series = TVDB::getSeries(73739);
$episodes = $series->getEpisodes(2);
示例 1 - 迭代所有集数
$page = 1;
do {
$episodes = TVDB::getSeriesEpisodes(73739, $page);
echo "Page $page has " . count($episodes) . " episodes. <br />";
$page++;
} while($episodes->hasNextPage());
/*
* Output:
* Page 1 has 100 episodes.
* Page 2 has 49 episodes.
*/
示例 2
$episodes = TVDB::getSeriesEpisodes(73739);
foreach($episodes as $episode) {
echo $episode->name . '<br />';
}
获取单个集数
/*
* Retrieve the episode with ID 127131
* .. returns an "Episode" object
*/
$episode = TVDB::getEpisode(127131);
echo $episode->name; // "Pilot (1)"
获取电影
/*
* Retrieve the movie with ID 1
* .. returns an "Movies" object
*/
$movie = TVDB::getMovies(1);
echo $movie->translations[14]->name; // "Alita: Battle Angel"
获取您的 TVDB JWT 令牌
有时检索您的 TVDB JWT 令牌(例如测试 API)可能很有用。
echo TVDB::getToken();
致谢
许可证
GPLv3 许可证(GPLv3)。有关更多信息,请参阅 许可证文件。