2017 © Pedro Peláez
 

library preview

Link preview generation for Empress.

image

empress/preview

Link preview generation for Empress.

  • Monday, September 4, 2017
  • by 19peaches
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 100 % Grown

The README.md

# Empress Preview

NOTICE

This is simply a re-packaged version of dusterio/link-preview ported over to use within the Empress fast scaffold framework, because we wanted to customize and add a couple features., (*1)

If you're building a normal Laravel app, please use Denis's version., (*2)

All original copyrights are included in the package and on every file., (*3)


Description

A PHP class that consumes an HTTP(S) link and returns an array of preview information. Think of Facebook sharing - whenever you paste a link, it goes to specified page and fetches some details., (*4)

Initially based on kasp3r/link-preview that seems to be abandoned., (*5)

Dependencies

  • PHP >= 5.5
  • Guzzle >= 6.1
  • Symfony DomCrawler >= 3.0

Installation via Composer

To install simply run:, (*6)

composer require empress/preview

Or add it to composer.json manually:, (*7)

{
    "require": {
        "empress/preview": "~1.0"
    }
}

Direct usage

use Empress\Preview\Client;

$previewClient = new Client('https://www.boogiecall.com/en/Melbourne');

// Get previews from all available parsers
$previews = $previewClient->getPreviews();

// Get a preview from specific parser
$preview = $previewClient->getPreview('general');

// Convert output to array
$preview = $preview->toArray();

Output, (*8)

array(4) {
  ["cover"]=>
  string(94) "https://cdn.boogiecall.com/media/images/872398e3d9598c494a2bed72268bf018_1440575488_7314_s.jpg"
  ["images"]=>
  array(8) {
    [0]=>
    string(94) "https://cdn.boogiecall.com/media/images/872398e3d9598c494a2bed72268bf018_1440575488_7314_s.jpg"
    [1]=>
    string(94) "https://cdn.boogiecall.com/media/images/b18970cd4c808f4dcdf7c319779ab9c6_1457347623_2419_s.jpg"
  }
  ["title"]=>
  string(44) "Events, parties & live concerts in Melbourne"
  ["description"]=>
  string(107) "List of events in Melbourne. Nightlife, best parties and concerts in Melbourne, event listings and reviews."
}

Timeouts and errors

// Default connect timeout is 5 seconds, you can change it to anything you want
$previewClient->getParser('general')->getReader()->config(['connect_timeout' => 3.14]);

// Default maximum redirect count is 10, but you can change it too
$previewClient->getParser('general')->getReader()->config(['allow_redirects' => ['max' => 10]]);

// If there is a network error (DNS, connect, etc), we throw ConnectionErrorException
try {
    $previews = $previewClient->getPreviews();
} catch (\Empress\Preview\Exceptions\ConnectionErrorException $e) {
    echo "Oh no!";
}

YouTube example

use Empress\Preview\Client;

$previewClient = new LinkPreview('https://www.youtube.com/watch?v=v1uKhwN6FtA');

// Only parse YouTube specific information
$preview = $previewClient->getPreview('youtube');

var_dump($preview->toArray());

Output, (*9)

array(2) {
  ["embed"]=>
  string(128) "<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/v1uKhwN6FtA" frameborder="0"/>"
  ["id"]=>
  string(11) "v1uKhwN6FtA"
}

Usage in Laravel 5

// Add in your config/app.php

'providers' => [
    '...',
    'Empress\Preview\Integrations\LaravelServiceProvider',
];

'aliases' => [
    '...',
    'Preview'    => 'Empress\Preview\Integrations\LaravelFacade',
];

// Set target url
Preview::setUrl('https://www.boogiecall.com');

// Get parsed HTML tags as a plain array
Preview::getPreview('general')->toArray();

// In case of redirects, see what the final url was
echo Preview::getUrl();

The Versions

04/09 2017

dev-master

9999999-dev http://github.com/periaptio/empress-preview

Link preview generation for Empress.

  Sources   Download

MIT

The Requires

 

laravel url php scraping preview empress

04/09 2017

v1.0

1.0.0.0 http://github.com/periaptio/empress-preview

Link preview generation for Empress.

  Sources   Download

MIT

The Requires

 

laravel url php scraping preview empress