Download the PHP package zyan/u2p without Composer

On this page you can find all versions of the php package zyan/u2p. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package u2p

zyan/u2p

网页图片抓取 SDK —— 输入网页链接,返回图片链接数组。

u2p = URL to Pictures。一个轻量、零外部依赖、可扩展的 PHP 图片抓取库,内置微信公众号文章等专项抓取,支持 ajax 接口与网页源码两种图片加载方式。

特性

环境要求

安装

快速开始

SDK 会根据 URL 自动匹配处理器:公众号链接走专项抓取,其它链接回退到通用抓取。

使用示例

1. 自动识别网站类型

2. 微信公众号文章 —— 含封面图

默认只返回正文图片,withCover() 会把封面图置于结果首位:

3. 通用网页抓取

适用于任意网站,解析页面源码 <img>,优先取懒加载地址,自动解析相对路径,过滤 base64:

4. 自定义处理器

为新的网站编写专项处理器(尤其适合 ajax 接口加载图片的网站):

完整可运行示例见 docs/test.php,执行 php docs/test.php 即可查看效果。

API 文档

U2P —— 入口管理器

方法 说明
get(string $url): string[] 抓取链接中的图片,返回图片链接数组
registerHandler(HandlerInterface $handler): self 注册专项处理器(先注册先匹配)
setFallback(HandlerInterface $handler): self 设置兜底处理器
getHandlers(): HandlerInterface[] 获取已注册的处理器列表
static make(?HttpClient $client = null): self 快速创建实例

WeChatHandler —— 公众号文章专项

方法 说明
handle(string $url): string[] 抓取正文图片
withCover(bool $withCover = true): self 是否将封面图置于结果首位
extractContentImages(string $html): string[] 从 HTML 提取正文图片(可独立使用)
supports(string $url): bool 判断是否为公众号链接

正文图片识别规则:

GenericHandler —— 通用兜底

方法 说明
handle(string $url): string[] 抓取任意网页图片
extractImages(string $html, string $baseUrl = ''): string[] 从 HTML 提取图片(可独立使用)

AbstractHandler —— 处理器基类

自定义处理器继承此类,可直接使用以下能力:

方法 说明
fetch(string $url, array $headers = []): string 抓取 HTML 内容
loadDom(string $html): DOMDocument 加载 HTML 为 DOM(自动 UTF-8 转码)
resolveSrc(DOMElement $img): string 解析 img 真实地址(懒加载优先)
cleanUrl(string $url): string 清理地址(去锚点等)
setClient(HttpClient $client): self 替换 HTTP 客户端

HttpClient —— HTTP 客户端

方法 说明
get(string $url, array $headers = [], array $options = []): string GET 请求
setDefaultHeaders(array $headers): self 设置默认请求头
setDefaultOptions(array $options): self 设置默认 cURL 选项

进阶用法

自定义 HTTP 客户端(代理 / 超时 / Cookie)

单独使用解析方法(不发起请求)

已有 HTML 内容时,可跳过 HTTP 直接解析:

架构

所有 Handler 实现 HandlerInterface

目录结构

测试

贡献

欢迎提交 Issue 和 PR!

新增网站专项处理器时,请:

  1. src/Handlers/ 下创建 XxxHandler.php,继承 AbstractHandler
  2. 实现 supports()handle()
  3. tests/ 添加测试用例与 fixture
  4. 确保所有测试通过:vendor/bin/phpunit

License

MIT License


All versions of u2p with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
guzzlehttp/guzzle Version ^6.5|^7.5
symfony/dom-crawler Version ^5.4
symfony/css-selector Version ^5.4
ext-json Version *
ext-mbstring Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zyan/u2p contains the following files

Loading the files please wait ...