Xem trước php

Một lớp PHP sử dụng liên kết HTTP(S) và trả về một mảng thông tin xem trước. Hãy nghĩ về việc chia sẻ trên Facebook - bất cứ khi nào bạn dán một liên kết, nó sẽ chuyển đến trang được chỉ định và tìm nạp một số chi tiết

Ban đầu dựa trên kasp3r/link-preview dường như bị bỏ rơi

Bao gồm tích hợp với. ấu trùng 5

phụ thuộc

  • PHP >= 5. 5
  • Đánh đố >= 6. 1
  • Symfony DomCrawler >= 3. 0

Cài đặt qua Composer

Để cài đặt chỉ cần chạy

composer require dusterio/link-preview

Hoặc thêm nó vào composer.json theo cách thủ công

{
    "require": {
        "dusterio/link-preview": "~1.2"
    }
}

sử dụng trực tiếp

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();

đầu ra

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."
}

Thời gian chờ và lỗi

// 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!";
}

ví dụ về YouTube

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());

đầu ra

array(2) {
  ["embed"]=>
  string(128) "