mpyw/opengraph

PHP 库,用于消费和发布 Open Graph 资源。

v0.1.4 2021-06-26 11:47 UTC

This package is auto-updated.

Last update: 2024-09-13 08:58:20 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

这是一个简单的库,用于从网页中读取 Open Graph 数据并生成 HTML 代码以发布自己的 Open Graph 对象。回退模式允许您从未实现 Open Graph 协议的网站读取数据。

使用此库,您可以轻松检索诸如元数据、YouTube 或 Vimeo 中的视频信息或 Flickr 中的图像信息等数据,而无需使用特定网站的 API,因为它们都实现了 Open Graph 协议。

有关 Open Graph 协议的信息,请参阅 ogp.me

需求

安装

composer require mpyw/opengraph

使用方法

从 URL 获取 Open Graph 数据

use Mpyw\OpenGraph\Consumer;

$consumer = new Consumer();
$object = $consumer->loadHtml(file_get_contents('http://www.youtube.com/watch?v=P422jZg50X4'));

// Basic information of the object
echo 'Title: ' . $object->title;                // Getting started with Facebook Open Graph
echo 'Site name: ' . $object->siteName;         // YouTube
echo 'Description: ' . $object->description;    // Originally recorded at the Facebook World ...
echo 'Canonical URL: ' . $object->url;          // http://www.youtube.com/watch?v=P422jZg50X4

// Images
$image = $object->images[0];
echo 'Image[0] URL: ' . $image->url             // https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg
echo 'Image[0] height: ' . $image->height       // null (May return height in pixels on other pages)
echo 'Image[0] width: ' . $image->width         // null (May return width in pixels on other pages)

// Videos
$video = $object->videos[0];
echo 'Video URL: ' . $video->url                // http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1
echo 'Video height: ' . $video->height          // 1080
echo 'Video width: ' . $video->width            // 1920
echo 'Video type: ' . $video->type              // application/x-shockwave-flash

还有一些其他属性,但这些是基本且最常用的。

发布自己的 Open Graph 数据

use Mpyw\OpenGraph\Elements\Image;
use Mpyw\OpenGraph\Elements\Video;
use Mpyw\OpenGraph\Publisher;
use Mpyw\OpenGraph\Objects\Website;

$publisher = new Publisher();
$object = new Website();

// Basic information of the object
$object->title = 'Getting started with Facebook Open Graph';
$object->siteName = 'YouTube';
$object->description = 'Originally recorded at the Facebook World ...'
$object->url = 'http://www.youtube.com/watch?v=P422jZg50X4';

// Images
$image = new Image('https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg');
$object->images[] = $image;

// Videos
$video = new Video('http://www.youtube.com/v/P422jZg50X4?version=3&autohide=1');
$video->height = 1080;
$video->width = 1920;
$video->type = 'application/x-shockwave-flash';
$object->videos[] = $video;

// Generate HTML code
echo $publisher->generateHtml($object);
// <meta property="og:description"
//       content="Originally recorded at the Facebook World ...">
// <meta property="og:image:url"
//       content="https://i1.ytimg.com/vi/P422jZg50X4/maxresdefault.jpg">
// <meta property="og:site_name"
//       content="YouTube">
// <meta property="og:type"
//       content="website">
// <meta property="og:url"
//       content="http://www.youtube.com/watch?v=P422jZg50X4">
// <meta property="og:video:url"
//       content="http://www.youtube.com/v/P422jZg50X4?version=3&amp;autohide=1">
// <meta property="og:video:height"
//       content="1080">
// <meta property="og:video:type"
//       content="application/x-shockwave-flash">
// <meta property="og:video:width"
//       content="1920">

HTML 代码仅用于显示目的。您可以使用 $publisher->doctype 属性选择 HTML5/XHTML 输出。

运行测试

您可以使用以下命令运行测试套件

phpunit --bootstrap tests/bootstrap.php .

许可

此库受 MIT 许可证的许可。