PHP code example of majo40 / link-preview

1. Go to this page and download the library: Download majo40/link-preview library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

majo40 / link-preview example snippets


use Dusterio\LinkPreview\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();

// 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 (\Dusterio\LinkPreview\Exceptions\ConnectionErrorException $e) {
    echo "Oh no!";
}

use Dusterio\LinkPreview\Client;

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

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

var_dump($preview->toArray());

// Add in your config/app.php

'providers' => [
    '...',
    'Dusterio\LinkPreview\Integrations\LaravelServiceProvider',
];

'aliases' => [
    '...',
    'Preview'    => 'Dusterio\LinkPreview\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();