simexis / embed
使用 oembed, opengraph 等获取页面信息的 PHP 库
Requires
- php: >=5.5.9
- ext-curl: *
- guzzlehttp/guzzle: ~5.3|~6.0.1|~6.1
README
# 嵌入
PHP 库,用于从任何网页获取信息(使用 oembed、opengraph、twitter-cards、scrapping html 等)。它与任何网络服务(youtube、vimeo、flickr、instagram 等)兼容,并提供了一些网站的适配器,如(archive.org、github、facebook 等)。
需求
- PHP 5.4+
- 已安装 Curl 库
用法
//Load library (if you don't have composer or any psr-4 compatible loader): include('src/autoloader.php'); //Load any url: $info = Embed\Embed::create('https://www.youtube.com/watch?v=PP1xn5wHtxE'); //Get content info $info->title; //The page title $info->description; //The page description $info->url; //The canonical url $info->type; //The page type (link, video, image, rich) $info->images; //List of all images found in the page $info->image; //The image choosen as main image $info->imageWidth; //The width of the main image $info->imageHeight; //The height of the main image $info->code; //The code to embed the image, video, etc $info->width; //The width of the embed code $info->height; //The height of the embed code $info->aspectRatio; //The aspect ratio (width/height) $info->authorName; //The (video/article/image/whatever) author $info->authorUrl; //The author url $info->providerName; //The provider name of the page (youtube, twitter, instagram, etc) $info->providerUrl; //The provider url $info->providerIcons; //All provider icons found in the page $info->providerIcon; //The icon choosen as main icon $info->publishedDate; //The (video/article/image/whatever) published date
自定义
您可以使用数组作为第二个参数来设置一些选项。在这个数组中,您可以配置适配器、提供者、解析器等。
适配器
适配器是从提供者获取页面所有信息的类,并为每个值选择最佳结果。例如,一个页面可以从 opengraph、twitter cards、oembed、<title>
html 元素等提供多个标题,因此适配器会获取所有这些标题并选择最佳的一个。
Embed 有一个通用的适配器“Webpage”,用于任何网站,但也为 archive.org、facebook、google、github、spotify、等网站提供了一些特定的适配器,这些网站使用它们自己的 api 或有其他特殊问题。
您可以配置这些适配器,甚至创建自己的适配器,该适配器必须实现 Embed\Adapters\AdapterInterface
。
适配器的可用选项有
- minImageWidth (int): 选择主图像的最小宽度
- minImageHeight (int): 选择主图像的最小高度
- imagesBlacklist (array): 您不想使用的图像。可以是纯文本或Url 匹配模式。
- getBiggerImage (bool): 选择较大的图像作为主图像(而不是首先找到的图像,通常是最相关的)。
- getBiggerIcon (bool): 与 getBiggerImage 相同,但用于选择主图标。
$config = [ 'adapter' => [ 'class' => 'MyCustomClass', //Your custom adapter 'config' => [ 'minImageWidth' => 16, 'minImageHeight' => 16, 'imagesBlacklist' => null, 'getBiggerImage' => false, 'getBiggerIcon' => false, ] ] ];
提供者
提供者从不同的来源获取数据。每个来源都有自己的提供者。例如,有一个提供者用于 open graph,其他用于 twitter cards、oembed、html 等。接收选项的提供者有
oembed
用于从可用的 oembed api 获取数据。它接受两个选项
- parameters (array): 要与 oembed 请求一起发送的额外查询参数
- embedlyKey (string): 如果已定义并且页面没有自己的 oembed 服务,则使用 embedly api。
html
用于从页面的 html 代码直接获取数据
- maxImages (int): 从 html 代码中获取的图像的最大数量(搜索
<img>
元素)。默认为 -1(无限制)。使用 0 不获取图像。
此提供者仅用于 facebook 页面,用于从 graph api 获取信息
- key (string): 从非公开页面获取信息的访问令牌
soundcloud
仅用于 soundcloud 页面,用于使用其 api 获取信息
- key (string): 从 soundcloud API 获取信息。
$config = [ 'providers' => [ 'oembed' => [ 'parameters' => [], 'embedlyKey' => null ], 'html' => [ 'maxImages' => -1 ], 'facebook' => [ 'key' => 'our-access-token' ] ] ];
请求解析器
Embed 使用 Embed\RequestResolvers\Curl
类来解析所有请求。您可以设置 curl 请求的选项或使用自定义解析器创建一个实现 Embed\RequestResolvers\RequestResolverInterface
的类。
解析器配置在“resolver”键下定义,它有两个选项
- class:如果您想使用自己的实现,请输入您自定义的类名
- config:传递给类的选项。如果您使用默认的curl类,配置与curl_setopt PHP函数相同
// CURL $config = [ 'resolver' => [ 'class' => 'Embed\\RequestResolvers\\Curl' // The default resolver used 'config' => [ CURLOPT_MAXREDIRS => 20, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_ENCODING => '', CURLOPT_AUTOREFERER => true, CURLOPT_USERAGENT => 'Embed PHP Library', CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, ] ] ]; // Guzzle (5.x) $config = [ 'resolver' => [ 'class' => 'Embed\\RequestResolvers\\Guzzle5', // Guzzle5 resolver used 'config' => [ // optional: if you need to use your custom Guzzle instance 'client' => $myGuzzleClient, ] ] ];
图片信息
为了检查图片并获取它们的mimetype和尺寸,我们使用了类Embed\ImageInfo\Curl
。此类使用curl发起请求,获取前几个字节以获取图片类型和尺寸,并关闭连接。因此,图片不会被完全下载,只会下载足够的数据以获取这些信息。
与解析器类类似,您可以使用自己的图片类(它必须实现Embed\ImageInfo\ImageInfoInterface
)或更改配置。可用的选项相同
- class:如果您想使用自己的实现,请输入您自定义的类名
- config:传递给类的选项。如果您使用默认的curl类,配置与curl_setopt PHP函数相同
$config = [ 'image' => [ 'class' => 'Embed\\ImageInfo\\Curl' //The default imageInfo used 'config' => [ CURLOPT_MAXREDIRS => 20, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_ENCODING => '', CURLOPT_AUTOREFERER => true, CURLOPT_USERAGENT => 'Embed PHP Library', CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4, ] ] ];
示例
$config = [ 'adapter' => [ 'config' => [ 'minImageWidth' => 16, 'minImageHeight' => 16, 'imagesBlacklist' => [ 'http://example.com/full/path/to/image.jpg', 'http?://test.*/*.png/', '*/bad_image.gif' ] ] ], 'providers' => [ 'html' => [ 'maxImages' => 3 ] ], 'resolver' => [ 'config' => [ CURLOPT_USERAGENT => 'My spider', CURLOPT_MAXREDIRS => 3 ] ] 'image' => [ 'class' => 'App\\MyImageInfoClass' ] ];
需要帮助吗?
我可以在HackHands上帮助您:https://hackhands.com/oscarotero