shweshi / opengraph
一个用于获取网站 Open Graph 元数据的 Laravel 扩展包。
Requires
- illuminate/log: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: 4.8.*
README
OpenGraph 是一个 Laravel 扩展包,用于获取网站/链接的 Open Graph 元数据。
功能
-
轻松获取 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
有关如何在 Laravel 中获取 Open Graph 元数据的文章可以在 Medium 博客上找到:[https://hackernoon.com/how-to-fetch-open-graph-metadata-in-laravel-2d5d674904d7](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', )
您还可以通过 URL 传递一个可选参数 true 或 false。当设置为 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);
注意
如果您在从社交媒体网站获取元数据时遇到问题,请使用以下用户代理设置在此示例上
$opg_array = OpenGraph::fetch('URL', true, null, null, 'facebookexternalhit/1.1 (+https://#/externalhit_uatext.php)');
异常处理
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)。有关更多信息,请参阅 许可证文件。