unl / oembedresource
生成要返回给oEmbed请求的oEmbed资源
1.0.0
2021-07-08 20:09 UTC
Requires
- php: >=7.3
- ext-dom: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
oEmbed资源
OembedResource
类生成用于返回给oEmbed请求的JSON和XML资源负载。
安装
使用composer安装此包
composer require unl/oembedresource
用法
use unl\OembedResource\OembedResource; ... // Instantiate OembedResource class and add oEmbed parameters as properties. $resource = new OembedResource('video'); $resource->setTitle('My Video'); $resource->setHtml('<iframe src="https://example.com/video/123/embed" title="My Video" allowfullscreen frameborder="0"></iframe>'); $resource->setWidth(940); $resource->setHeight(549); $resource_payload = $resource->generate(); // At this point, return an HTTP response. header('Content-Type: application/json+oembed'); echo $resource_payload; die();
实例化OembedResource类
构造函数需要一个'type'参数来指定请求的资源类型。有效的类型包括:photo
、video
、link
和rich
。
$resource = new OembedResource('video');
将oEmbed参数作为类属性添加
该类提供GETTER和SETTER方法
// Type // Required to instantiate class. $type= $resource->getType(); // Title (string) // Optional $resource->setTitle('My Video'); $title = $resource->getTitle(); // Author Name (string) // Optional $resource->setAuthorName('Mark Twain'); $author_name = $resource->getAuthorName(); // Author URL (string) // Optional $resource->setAuthorUrl('https://www.my-oembed-provider-service.com/mtwain'); $author_url = $resource->getAuthorUrl(); // Provider Name (string) // Optional $resource->setProviderName('My oEmbed Provider'); $provider_name = $resource->getProviderName(); // Provider URL (string) // Optional $resource->setProviderUrl('https://www.my-oembed-provider-service.com'); $provider_url = $resource->getProviderUrl(); // Cache Age (int) // Optional $resource->setCacheAge(86400); $cache_age = $resource->getCacheAge(); // Thumbnail URL (string) // Optional (If any thumbnail oEmbed parameter is set, then all three must be set.) $resource->setThumbnailUrl('https://www.my-oembed-provider-service.com/video/123/thumbnail.jpg'); $thumbnail_url = $resource->getThumbnailUrl(); // Thumbnail Width (int) // Optional (If any thumbnail oEmbed parameter is set, then all three must be set.) $resource->setThumbnailWidth(940); $thumbnail_width = $resource->getThumbnailWidth(); // Thumbnail Height (int) // Optional (If any thumbnail oEmbed parameter is set, then all three must be set.) $resource->setThumbnailHeight(549); $thumbnail_height = $resource->getThumbnailHeight(); // URL (string) // Required for 'photo' resources. $resource->setUrl('https://www.my-oembed-provider-service.com/photo/123.jpg'); $url = $resource->getUrl(); // Width (int) // Required for 'photo', 'video', and 'rich' resources. $resource->setWidth(940); $width = $resource->getWidth(); // Height (int) // Required for 'photo', 'video', and 'rich' resources. $resource->setHeight(940); $height = $resource->getHeight(); // HTML (string) // Required for 'video' and 'rich' resources. $resource->setHtml('<iframe src="https://example.com/video/123/embed" title="My Video" allowfullscreen frameborder="0"></iframe>'); $html = $resource->getHtml();
生成响应负载
要将OembedResource
对象转换为JSON或XML字符串,请调用generate()
方法
// By default, JSON is generated. $json_payload = $resource->generate(); $json_payload = $resource->generate('json'); // XML can also be generated. $xml_payload = $resource->generate('xml');
向客户端发送响应
一旦生成了负载字符串,返回HTTP响应给客户端的责任就由实例化代码承担。请查阅您的PHP框架文档(Laravel、Symfony等)。
以下提供了JSON和XML的快速返回示例
// Send JSON response. $resource_payload = $resource->generate(); header('Content-Type: application/json+oembed'); echo $resource_payload; die();
// Send XML response. $resource_payload = $resource->generate('xml'); header('Content-Type: text/xml+oembed'); echo $resource_payload; die();