devcoda25 / redtubeapi
RedTube API 开发者文档!在这里您可以找到获取数据以及将 RedTube 视频集成到您的网站或应用所需的所有详细信息
1.1.0
2024-08-24 08:47 UTC
Requires
- doctrine/common: ^3.4
- guzzlehttp/guzzle: ^7.9
- illuminate/support: ^10.48
- jms/serializer: ^3.30
- myclabs/php-enum: ^1.8
README
PHP 基础的 Redtube HTTP API 包装器。
除了 getStarDetailedList
之外的所有方法都受支持。在编写本文时,该方法总是以 没有星星! 错误结束。
安装
composer require devcoda25/redtubeapi
使用
每个方法都返回一个代表响应的具体对象或对象集合。用于过滤视频的方法返回一个包含所有视频和分页基本信息的 Paginator
对象(当前页、总计数、最后一页等)。
// Create new API client (default host points to https://api.redtube.com)
$redtube = new \devcoda25\Redtube\Redtube;
/**
* Get all categories
*
* @return Collection<Category>
*/
$redtube->categories->getAll();
/**
* Get all stars
*
* @return Collection<Star>
*/
$redtube->stars->getAll();
/**
* Get all tags
*
* @return Collection<Tag>
*/
$redtube->tags->getAll();
/**
* Find videos
*
* @return Paginator
*/
$redtube->videos->findBy($filter);
/**
* Find video by its ID
*
* @return Video
*/
$redtube->videos->findById($id);
/**
* Find out if a video is active
*
* @return bool
*/
$redtube->videos->isActive($id);
/**
* Get embed code of a video
*
* @return string
*/
$redtube->videos->getEmbedCode($id);
/**
* Find deleted videos
*
* @return Paginator
*/
$redtube->videos->findDeletedBy($filter);
视频过滤器
可以使用提供流畅接口的 VideoFilter
类来过滤视频。您可以使用三种枚举器之一来简化过滤过程 - Period
、OrderBy
、Thumbsize
。
注意:Redtube 不使用传统的 limit/offset 查询,而是通过 页码 进行过滤。每页包含 20 个结果。
例如
$filter = VideoFilter::create()
->page(1)
->search('lesbian')
->tags(['blonde'])
->stars(['Jenna Jameson'])
->thumbsize(Thumbsize::BIG)
->period(Period::ALL_TIME)
->orderBy(OrderBy::RATING);
$videos = $redtube->videos->findBy($filter);
获取所有活跃/已删除的视频
如果您想一次性检索多页(超过 20 个视频),可以使用以下方法
/**
* Find all videos up to 10th page
*
* @return Generator
*/
$redtube->videos->findAllBy($filter, 10);
/**
* Find deleted videos up to the 10th page
*
* @return Generator
*/
$redtube->videos->findAllDeletedBy($filter, 10);
两种方法在请求每一页后都返回一个 生成器,以简化内存分配。所以完整的用法示例将是
foreach ($redtube->videos->findAllBy($filter, 10) as $videos) {
/** @var Illuminate\Support\Collection $videos */
$videos->each(function (Video $video) {
// rest of the code
});
}
异常
所有列出的错误代码都通过以下映射转换为 PHP 异常
- 错误代码 1001 =>
NoSuchMethodException
- 错误代码 1002 =>
NoSuchDataProviderException
- 错误代码 1003 =>
NoInputParametersSpecifiedException
- 其余的错误代码将使用
NotFoundException
并带有适当的消息
例如
try {
$videos = $redtube->videos->findBy($filter);
} catch (NotFoundException $e) {
}