jclyons52 / page-preview
获取页面链接信息并生成预览图片
v1.0.0
2016-05-04 03:21 UTC
Requires
- php: ~5.5|~7.0
- jclyons52/php-query: ^1.0@dev
- league/plates: ^3.1
- psr/cache: ~1.0
Requires (Dev)
- phpunit/phpunit: 4.*
- squizlabs/php_codesniffer: ~2.3
- tedivm/stash: ^0.14.1
This package is not auto-updated.
Last update: 2024-09-14 19:19:54 UTC
README
创建带缩略图、标题和描述的页面链接预览
安装
通过 Composer
$ composer require jclyons52/page-preview
使用方法
$previewManager = PreviewManager::create(); $preview = $previewManager->fetch('https://somewebsite.com'); echo $preview->render(); // returns bootstrap media link preview echo $preview->toJson(); // returns json string of preview attributes
或者直接在行内操作
PreviewManager::create()->fetch('https://somewebsite.com')->render();
使用其他默认模板之一
$previewManager->render('thumbnail');
定义自己的模板
$previewManager->fetch('https://somewebsite.com')->render('myAwesomeTemplate', '/path/to/template/directory');
缓存
由于 HTTP 调用速度较慢,因此为了提高响应速度,您可能希望缓存预览。此包可以将任何 psr-6 兼容的缓存驱动器作为参数。
$pool = new Pool(); $previewManager = PreviewManager::create($pool); $preview = $previewManager->findOrFetch('http://www.example.com/directory'); $previewManager->cache($preview);
您模板中可用的数据将包括
- 字符串 $title - 元标题或如果未在元中找到则使用页面标题
- 字符串 $description - 元描述
- 字符串 $url - 链接 URL
- 数组 $images - 图片 URL 数组
- 数组 $meta - 以名称为键的元值数组
如果您使用来自标签的信息,例如 Twitter 元标签(或用冒号分隔的任何内容),您可能希望使用 unFlatten 函数来获取多级数组。
此元
<meta name="twitter:card" content="app"> <meta name="twitter:site" content="@TwitterDev"> <meta name="twitter:description" content="Cannonball is the fun way to create and share stories and poems on your phone. Start with a beautiful image from the gallery, then choose words to complete the story and share it with friends."> <meta name="twitter:app:country" content="US"> <meta name="twitter:app:name:iphone" content="Cannonball"> <meta name="twitter:app:id:iphone" content="929750075"> <meta name="twitter:app:url:iphone" content="cannonball://poem/5149e249222f9e600a7540ef"> <meta name="twitter:app:name:ipad" content="Cannonball"> <meta name="twitter:app:id:ipad" content="929750075"> <meta name="twitter:app:url:ipad" content="cannonball://poem/5149e249222f9e600a7540ef"> <meta name="twitter:app:name:googleplay" content="Cannonball"> <meta name="twitter:app:id:googleplay" content="io.fabric.samples.cannonball"> <meta name="twitter:app:url:googleplay" content="http://cannonball.fabric.io/poem/5149e249222f9e600a7540ef">
使用 unFlatten
$meta = $preview->meta->unFlatten()['twitter'];
将产生以下数组
[ "card" => "app", "site" => "@TwitterDev", "description" => "Cannonball is the fun way to create and share stories and poems on your phone. Start with a beautiful image from the gallery, then choose words to complete the story and share it with friends.", "app" => [ "country" => "US", "name" => [ "iphone" => "Cannonball", "ipad" => "Cannonball", "googleplay" => "Cannonball", ], "id" => [ "iphone" => "929750075", "ipad" => "929750075", "googleplay" => "io.fabric.samples.cannonball", ], "url" => [ "iphone" => "cannonball://poem/5149e249222f9e600a7540ef", "ipad" => "cannonball://poem/5149e249222f9e600a7540ef", "googleplay" => "http://cannonball.fabric.io/poem/5149e249222f9e600a7540ef", ], ] ];
变更日志
请参阅 CHANGELOG 了解最近的变化信息。
测试
$ composer test
贡献
请参阅 CONTRIBUTING 和 CONDUCT 了解详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 jclyons52@gmail.com 而不是使用问题跟踪器。
鸣谢
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件。