ricardofiorani/php-video-url-parser

此包已被废弃,不再维护。作者建议使用ricardofiorani/oembed包。

一个简单高效的PHP视频URL解析器,为YouTube、Vimeo、Dailymotion和Facebook等提供缩略图和嵌入代码。

v1.0.2 2019-07-28 14:12 UTC

README

如果你有PHP 7.4或更高版本,请使用https://github.com/ricardofiorani/oembed代替。
此库将仅接收小故障修复,不再添加新功能。

PHP视频URL解析器

Build Status Minimum PHP Version License Total Downloads Coding Standards Scrutinizer Code Quality Code Coverage

PHP视频URL解析器是一个解析器,它检测给定的视频URL并返回一个包含视频嵌入代码、标题、描述、缩略图以及其他服务API可能提供的信息的对象。

安装

使用以下命令安装最新版本:

$ composer require ricardofiorani/php-video-url-parser

要求

  • PHP 5.3
  • cURL(或者如果你想与Vimeo一起使用,至少启用file_get_contents())

基本用法

<?php
use RicardoFiorani\Matcher\VideoServiceMatcher;

require __DIR__ . '/vendor/autoload.php';

$vsm = new VideoServiceMatcher();

//Detects which service the url belongs to and returns the service's implementation
//of RicardoFiorani\Adapter\VideoAdapterInterface
$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');

//Checks if service provides embeddable videos (most services does)
if ($video->isEmbeddable()) {
    //Will echo the embed html element with the size 200x200
    echo $video->getEmbedCode(200, 200);

    //Returns the embed html element with the size 1920x1080 and autoplay enabled
    echo $video->getEmbedCode(1920, 1080, true);
    
    //Returns the embed html element with the size 1920x1080, autoplay enabled and force the URL schema to be https.
    echo $video->getEmbedCode(1920, 1080, true, true);
}

//If you don't want to check if service provides embeddable videos you can try/catch
try {
    echo $video->getEmbedUrl();
} catch (\RicardoFiorani\Exception\NotEmbeddableException $e) {
    die(sprintf("The URL %s service does not provide embeddable videos.", $video->getRawUrl()));
}

//Gets URL of the smallest thumbnail size available
echo $video->getSmallThumbnail();

//Gets URL of the largest thumbnail size available
//Note some services (such as Youtube) does not provide the largest thumbnail for some low quality videos (like the one used in this example)
echo $video->getLargestThumbnail();

注册自己的服务视频(很简单!)

如果你想注册某个服务的实现,你的类只需要实现 "RicardoFiorani\Adapter\VideoAdapterInterface" 或扩展 "RicardoFiorani\Adapter\AbstractServiceAdapter"。

一个完整功能的示例可以在这里找到。

PS:如果你已经实现了一些知名服务的出色版本,请随时发送Pull Request。所有贡献都受欢迎:)

使用你自己的框架模板引擎

一个完整功能的示例可以在这里找到。

当前支持的服务

  • YouTube
  • Vimeo
  • Dailymotion
  • Facebook视频

当前支持的PHP版本

  • PHP 5.3
  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • PHP 7.0
  • PHP 7.1
  • PHP 7.2

请注意,此库在HHVM上无法通过测试,因此我们无法保证它将正常工作。请在自己的风险下使用。