tdonselaar / opengraph
一个用于抓取网站 Open Graph 元数据的 Laravel 包。
Requires
- ext-curl: *
- ext-dom: *
- ext-json: *
- ext-libxml: *
- illuminate/log: ^5.0|^6.0|^7.0|^8.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
OpenGraph 是一个用于抓取网站/链接的 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 "shweshi/opengraph"
如果您没有运行 Laravel 5.5(或更高版本),请将服务提供者在 config/app.php 中添加
-
添加服务提供者 打开
config/app.php
并将shweshi\OpenGraph\Providers\OpenGraphProvider::class,
添加到providers
数组的末尾'providers' => array( .... shweshi\OpenGraph\Providers\OpenGraphProvider::class, ),
接下来在
aliases
数组下'aliases' => array( .... 'OpenGraph' => shweshi\OpenGraph\Facades\OpenGraphFacade::class ),
如果您在 Laravel 5.5+ 上运行该包,则自动发现机制会处理添加服务提供者的魔法。
要求
- 您需要安装 DOM 扩展。
如何使用
-
按照上述步骤操作后,
use OpenGraph; $data = OpenGraph::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 = OpenGraph::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);
您还可以将额外的 Libxml 参数作为第四个参数($options)传递 https://php.ac.cn/manual/en/libxml.constants.php。默认选项设置为抑制错误和警告的报道
$data = OpenGraph::fetch($url, $allMeta, $language, $options);
您可以使用第五个参数设置请求的用户代理($userAgent)。默认为 'Curl'。
$data = OpenGraph::fetch($url, $allMeta, $language, $options, $userAgent);
异常处理
fetch() 方法在失败时返回一个包含额外数据的 FetchException。
try {
$data = OpenGraph::fetch($url, true);
} catch (shweshi\OpenGraph\Exceptions\FetchException $e) {
$message = $e->getMessage();
$data = $e->getData();
}
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 和 CODE OF CONDUCT 了解详细信息。
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。