cloudvisionbulgaria/link-preview

PHP 的链接预览库

v2.0.1 2020-08-18 20:28 UTC

This package is not auto-updated.

Last update: 2024-09-26 15:17:45 UTC


README

一个 PHP 库,可以轻松地从给定的 URL 获取网站信息(标题、描述、图片...)。

依赖

  • PHP >= 5.6(没有 phpunit 也可以在 5.5 上运行)
  • Guzzle

安装

composer

要使用 composer 安装 LinkPreview,您需要在项目根目录下创建 composer.json 并添加以下内容:

{
    "require": {
        "cloudvisionbulgaria/link-preview": "dev-master"
    }
}

然后运行

$ wget -nc https://getcomposer.org.cn/composer.phar
$ php composer.phar install

库将安装在 vendor/cloudvisionbulgaria/link-preview

在您的项目中,从 vendor/autoload.php 包含 composer 自动加载文件

用法

use LinkPreview\LinkPreview;

$linkPreview = new LinkPreview('http://github.com');
$parsed = $linkPreview->getParsed();
foreach ($parsed as $parserName => $link) {
    echo $parserName . PHP_EOL . PHP_EOL;

    echo $link->getUrl() . PHP_EOL;
    echo $link->getRealUrl() . PHP_EOL;
    echo $link->getTitle() . PHP_EOL;
    echo $link->getDescription() . PHP_EOL;
    echo $link->getImage() . PHP_EOL;
    print_r($link->getPictures());
}

输出

general

http://github.com
https://github.com/
GitHub · Build software better, together.
GitHub is the best place to build software together. Over 10.1 million people use GitHub to share code.
https://assets-cdn.github.com/images/modules/open_graph/github-octocat.png
Array
(
    [0] => https://assets-cdn.github.com/images/modules/site/home-ill-build.png?sn
    [1] => https://assets-cdn.github.com/images/modules/site/home-ill-work.png?sn
    [2] => https://assets-cdn.github.com/images/modules/site/home-ill-projects.png?sn
    [3] => https://assets-cdn.github.com/images/modules/site/home-ill-platform.png?sn
    [4] => https://assets-cdn.github.com/images/modules/site/org_example_nasa.png?sn
)

### YouTube 示例

use LinkPreview\LinkPreview;
use LinkPreview\Model\VideoLink;

$linkPreview = new LinkPreview('https://www.youtube.com/watch?v=8ZcmTl_1ER8');
$parsed = $linkPreview->getParsed();
foreach ($parsed as $parserName => $link) {
    echo $parserName . PHP_EOL . PHP_EOL;

    echo $link->getUrl() . PHP_EOL;
    echo $link->getRealUrl() . PHP_EOL;
    echo $link->getTitle() . PHP_EOL;
    echo $link->getDescription() . PHP_EOL;
    echo $link->getImage() . PHP_EOL;
    if ($link instanceof VideoLink) {
        echo $link->getVideoId() . PHP_EOL;
        echo $link->getEmbedCode() . PHP_EOL;
    }
}

输出

youtube

https://www.youtube.com/watch?v=8ZcmTl_1ER8
http://gdata.youtube.com/feeds/api/videos/8ZcmTl_1ER8?v=2&alt=jsonc
Epic sax guy 10 hours
I had to remove my original one so I reuploaded this with much better quality.
(If you want it sound like previous one, try setting quality to 240p)
Yeah, I know that video sucks compared to original but no can do :(
http://i1.ytimg.com/vi/8ZcmTl_1ER8/hqdefault.jpg
8ZcmTl_1ER8
<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/8ZcmTl_1ER8" frameborder="0"/>

待办事项

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

许可证

MIT 许可证 (MIT)

版权所有 (c) 2013 Tadas Juozapaitis kasp3rito@gmail.com

在此条件下,免费向任何获得此软件及其相关文档副本(以下简称“软件”)的人授予无条件的许可,允许其在不受限制的软件中进行处理,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供者提供软件的人按以下条件进行操作

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

软件按“现状”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任基于合同、侵权或其他方式,以及与软件或软件的使用或其他交易有关或产生。