realshadow / redtube-api

基于PHP的Redtube HTTP API包装器

1.0.2 2017-12-04 20:13 UTC

This package is not auto-updated.

Last update: 2024-09-15 03:13:48 UTC


README

Scrutinizer Code Quality Build Status

Redtube API

基于PHP的Redtube HTTP API包装器。

除了getStarDetailedList方法外,所有方法都受支持。此方法在撰写本文时始终以没有星标!错误结束。

安装

composer require realshadow/redtube-api

使用

每个方法返回一个代表响应的具体对象或对象集合。用于过滤视频的方法返回一个包含所有视频和分页基本信息的Paginator对象(当前页、总数、最后一页等)。

// Create new API client (default host points to https://api.redtube.com)
$redtube = new \Realshadow\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类来过滤视频。您可以使用三种枚举器之一来简化过滤 - PeriodOrderByThumbsize

注意: 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) {

}