cohensive/oembed

使用OEmbed协议生成媒体嵌入。

v0.18.1 2024-04-28 07:15 UTC

README

Scrutinizer Code Quality Build Status Code Intelligence Status

OEmbed

PHP库,用于从支持OEmbed数据格式的各种媒体提供商检索和显示嵌入媒体。例如YouTube、Vimeo、Twitter、Imgur等网站。

此库还支持直接mp4/video源和一些不使用OEmbed的媒体提供商。

安装

此库是用PHP 8编写的,具有许多PHP 8提供的整洁功能,因此对那些想要在PHP先前版本中使用它的人表示歉意。如果您确实需要类似库,我可以推荐检查我在这个领域的先前包:cohensive/embed

将以下require添加到您的composer.json文件中

"cohensive/oembed": "dev-master"
// or
"cohensive/oembed": "^0.16"

然后运行composer installcomposer update来下载它并自动加载。

或者运行

$ composer require cohensive/oembed

如果您正在安装此库并希望与Laravel一起使用,包应自动加载其Service Provider。

如果您计划使用Laravel的OEmbed外观,请将外观添加到您的app.php配置文件中的别名部分

'aliases' => array(

	//...
	'OEmbed' => 'Cohensive\OEmbed\Facades\OEmbed'
	//...

)

用法

独立使用

// Use of factory will automatically load list of providers.
$factory = new \Cohensive\OEmbed\Factory();
$embed = $factory->get('http://youtu.be/uifYHNyH-jA');

if ($embed) {
	// Print default embed html code.
	echo $embed->html();

	// Print embed html code with custom width. Works for IFRAME and VIDEO html embed code.
	echo $embed->html(['width' => 600]);

	// Checks if embed data contains details on thumbnail.
	$embed->hasThumbnail();

	// Returns embed "src" - URL string / array of strings / null for current embed.
	// Accepts same options as "html" method.
	$embed->src();

	// Returns an array containing thumbnail details: url, width and height.
	$embed->thumbnail();

	// Returns an array containing all available embed data including default HTML code.
	$embed->data();
}

Laravel使用

// Either use Facade:
$embed = OEmbed::get('http://youtu.be/uifYHNyH-jA');

// Load via Dependency Injection:
public function method(OEmbed $oembed) {
	$embed = OEmbed::get('http://youtu.be/uifYHNyH-jA');
}

if ($embed) {
	// Print default embed html code.
	echo $embed->html();

	// Print embed html code with custom width. Works for IFRAME and VIDEO html embed code.
	echo $embed->html(['width' => 600]);

	// Checks if embed data contains details on thumbnail.
	$embed->hasThumbnail();

	// Returns an array containing thumbnail details: url, width and height.
	$embed->thumbnail();

	// Return thumbnail url if it exists or null.
	$embed->thumbnailUrl();

	// Returns an array containing all available embed data including default HTML code.
	$embed->data();
}

配置

此库在resources/文件夹中包含一个大的配置文件。该文件包含一个数组,您可以在全局范围内指定一些内容,并允许您选择要使用的媒体提供商和不要使用的媒体提供商。

如果您使用Laravel使用库,配置文件将自动加载,您还可以发布和编辑它。

$ php artisan vendor:publish

如果您以独立模式使用OEmbed,您可以将自己的配置文件添加到Factory或OEmbed类中。

重要更改

配置格式已更改为使用snake_case数组键,而不是之前使用的camelCase