page-preview
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
Creates link previews to pages with thumbnail, title and description, (*2)
demo, (*3)
Install
Via Composer, (*4)
``` bash
$ composer require jclyons52/page-preview, (*5)
## Usage
``` php
$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
or do it inline:, (*6)
PreviewManager::create()->fetch('https://somewebsite.com')->render();
Use another one of the other default templates:, (*7)
$previewManager->render('thumbnail');
define your own templates:, (*8)
$previewManager->fetch('https://somewebsite.com')->render('myAwesomeTemplate', '/path/to/template/directory');
caching
Http calls are slow, so to speed up your response times you may want to cache your previews.
This package can take any psr-6 compliant cache driver as a parameter., (*9)
$pool = new Pool();
$previewManager = PreviewManager::create($pool);
$preview = $previewManager->findOrFetch('http://www.example.com/directory');
$previewManager->cache($preview);
The data available for you templates will be:, (*10)
- string $title - meta title or page title if not found in meta
- string $description - meta description
- string $url - link url
- array $images - array of image urls
- array $meta - array of meta values with their names as keys
If you're usign information from tags such as the twitter meta tags (or anything seperated with ':') you may want to use the unFlatten function to get a multi level array., (*11)
This meta:, (*12)
<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">
using unFlatten:, (*13)
$meta = $preview->meta->unFlatten()['twitter'];
Would produce the following array:, (*14)
[
"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",
],
]
];
Change log
Please see CHANGELOG for more information what has changed recently., (*15)
Testing
bash
$ composer test, (*16)
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*17)
Security
If you discover any security related issues, please email jclyons52@gmail.com instead of using the issue tracker., (*18)
Credits
License
The MIT License (MIT). Please see License File for more information., (*19)