dusterio / link-preview

支持 Laravel 的 PHP 链接预览生成

v1.2.18 2022-03-08 01:01 UTC

README

Build Status Code Climate Test Coverage Total Downloads Latest Stable Version Latest Unstable Version License

一个 PHP 类,它消耗一个 HTTP(S) 链接并返回一个包含预览信息的数组。想想 Facebook 分享——每次你粘贴一个链接,它就会跳转到指定的页面并获取一些详细信息。

最初基于 kasp3r/link-preview,似乎已被遗弃。

包括以下集成:Laravel 5

依赖

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

通过 Composer 安装

要安装,只需运行

composer require dusterio/link-preview

或将它手动添加到 composer.json

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

直接使用

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

输出

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

超时和错误

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

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

输出

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

Laravel 5 中的使用

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

待办事项

  1. 添加更多单元和集成测试
  2. 更新文档
  3. 添加更多解析器

许可证

MIT 许可证 (MIT) 版权所有 (c) 2016 Denis Mysenko

以下任何人可以免费获得此软件及其相关文档副本(“软件”),以无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供的人这样做,前提是必须遵守以下条件

上述版权声明和本许可声明应包含在软件的任何副本或主要部分中。

本软件按“原样”提供,除非另有声明,否则不提供任何形式的保证,无论是明示的、暗示的、还是基于任何特定目的的适用性或非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论这些责任是基于合同、侵权或其他方式,源自、涉及或与软件或其使用或任何其他交易有关。