quangtam / opengraph
一个用于获取网站 Open Graph 元数据的 Laravel 扩展包。
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
一个用于获取网站 Open Graph 元数据的 Laravel 扩展包。
特性
-
轻松获取 URL 的元数据。 Laravel OpenGraph 可以获取 URL 的所有元数据。
-
支持特定语言的元数据。 如果网页支持,Laravel OpenGraph 可以获取特定语言的元数据。
-
支持 Twitter 元数据。 Laravel OpenGraph 也支持 Twitter OG 数据。
-
验证图像 URL。 Laravel OpenGraph 会验证图像元数据中的图像 URL 是否有效。
演示
curl https://laravelopengraph.herokuapp.com/api/fetch?url=ogp.me&allMeta=true&language=en_GB
如何使用 Laravel OpenGraph
相关文章可在 Medium 博客上找到: https://hackernoon.com/how-to-fetch-open-graph-metadata-in-laravel-2d5d674904d7
安装
按照以下步骤使用此包:
- 通过 composer 安装
composer require "quangtam/opengraph"
如果您未运行 Laravel 5.5(或更高版本),则在 config/app.php 中添加服务提供者
-
添加服务提供者 打开
config/app.php
并将shweshi\OpenGraph\Providers\OpenGraphProvider::class,
添加到providers
数组的末尾'providers' => array( .... quangtam\OpenGraph\Providers\OpenGraphProvider::class, ),
然后在
aliases
数组下'aliases' => array( .... 'OGMeta' => quangtam\OpenGraph\Facades\OpenGraphFacade::class ),
如果您正在运行 Laravel 5.5+ 的包,包自动发现将处理添加服务提供者的魔法。
要求
- 您需要安装 DOM 扩展。
使用方法
-
在遵循以上步骤之后,
use OGMeta; $data = OGMeta::fetch("https://unsplash.com/");
这将返回一个类似以下的数组...
array ( 'title' => 'Beautiful Free Images & Pictures | Unsplash', 'description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.', 'type' => 'website', 'url' => 'https://unsplash.com/', 'image' => 'http://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0', 'image:secure_url' => 'https://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0', )
您还可以传递一个可选参数,即 true 或 false,与 URL 一起。当设置为 false 时,它将只获取基本元数据;当设置为 true 时,它将获取所有其他可选元数据,如音频、视频、音乐和 Twitter 元标签。
$data = OGMeta::fetch("https://unsplash.com/", true);
这将返回一个类似以下的数组...
array ( 'charset' => 'UTF8', 'viewport' => 'width=device-width, initial-scale=1.0, maximum-scale=1.0, minimal-ui', 'mobile-web-app-capable' => 'yes', 'apple-mobile-web-app-capable' => 'yes', 'apple-mobile-web-app-title' => 'Unsplash', 'application-name' => 'Unsplash', 'author' => 'Unsplash', 'msapplication-config' => 'browserconfig.xml', 'msapplication-TileColor' => '#ffffff', 'msapplication-TileImage' => 'https://unsplash.com/mstile-144x144.png', 'theme-color' => '#ffffff', 'description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.', 'twitter:site' => '@unsplash', 'twitter:title' => 'Beautiful Free Images & Pictures | Unsplash', 'twitter:description' => 'Beautiful, free images and photos that you can download and use for any project. Better than any royalty free or stock photos.', 'twitter:url' => 'https://unsplash.com/', 'twitter:card' => 'summary_large_image', 'twitter:image' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9', 'title' => 'Beautiful Free Images & Pictures | Unsplash', 'type' => 'website', 'url' => 'https://unsplash.com/', 'image' => 'http://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9', 'image:secure_url' => 'https://images.unsplash.com/photo-1546486610-e9fe4f1e6751?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9', )
要获取特定语言的元数据,可以将语言作为第三个参数传递,该值将用作 Accept-Language 标头。
$url = "https://ogp.me", $allMeta = true, // can be false $language = 'en' // en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4 $data = OpenGraph::fetch($url, $allMeta, $language);
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
贡献
有关详细信息,请参阅 CONTRIBUTING 和 CODE OF CONDUCT
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件