定制 milivojsa/chrome-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

milivojsa/chrome-php

Composer 安装命令:

composer require milivojsa/chrome-php

包简介

A PHP Wrapper for Chrome Headless. Get the DOM of any webpage.

README 文档

README

Version Build Status StyleCI

Get the DOM of any webpage by using headless Chrome. Inspired by Browsershot.

Requirements

This package requires the Puppeteer Chrome Headless Node library. If you want to install it on Ubuntu 16.04 you can do it like this:

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
sudo npm install --global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium

Installation

To add this package to your project, you can install it via composer by running

composer require milivojsa/chrome-php

Usage

Here is a quick example how to use this package:

use ChromeHeadless\ChromeHeadless;

$html = ChromeHeadless::url('https://example.com')->getHtml();

Instead of getting the DOM as a string, you can also use thegetDOMCrawler() method, which will return a Symfony\Component\DomCrawler\Crawler instance.

use ChromeHeadless\ChromeHeadless;

$dom = ChromeHeadless::url('https://example.com')->getDOMCrawler();
    
$title = $dom->filter('title')->text();

This makes it easy to filter the DOM for specific elements. Check the full documentation here.

Timeout

You can specify a timeout after which the process will be killed. The timeout should be given in seconds.

ChromeHeadless::url('https://example.com')
                ->setTimeout(10)
                ->getDOMCrawler();

If the process runs out of time a Symfony\Component\Process\Exception\ProcessTimedOutException will be thrown.

Custom Chrome Path

You can specify a custom path to your Chrome installation.

ChromeHeadless::url('https://example.com')
                ->setChromePath('/path/to/chrome')
                ->getDOMCrawler();

Custom User Agent

You can specify a custom user agent. By default the standard Chrome Headless user agent will be used.

ChromeHeadless::url('https://example.com')
                ->setUserAgent('nice-user-agent')
                ->getDOMCrawler();

Custom Headers

You can specify custom headers which will be used for the request.

ChromeHeadless::url('https://example.com')
                ->setHeaders([
                    'DNT' => 1 // DO NOT TRACK
                ])
                ->getDOMCrawler();

Blacklist

You can specify a list of regular expressions for files that should not be loaded when you request a website. These expressions will be checked against the url of the file. Default behaviour of the method setBlacklist(array $blacklist, $clean = false) is to merge array passed as $blacklist with current blacklist property. If you want to override this default behaviour then you can set parameter $clean to be true.

ChromeHeadless::url('https://example.com')
                ->setBlacklist([
                    'www.example.com'
                ])
                ->setBlacklist([
                    'www.google-analytics.com',
                    'analytics.js'
                ]) // property blacklist now will have www.example.com and those two
                ->getDOMCrawler();
ChromeHeadless::url('https://example.com')
                ->setBlacklist([
                    'www.google-analytics.com',
                    'analytics.js'
                ])
                ->setBlacklist([
                    'www.example.com'
                ], true) // property blacklist now will only have www.example.com 
                ->getDOMCrawler();

Excluded

You can specify a list of resource types that should not be loaded when you request a website. These resource types will be checked against the resource type of the file. You can pass values: document, stylesheet, image, media, font and script. Default behaviour of the method setExcluded(array $excluded, $clean = false) is to merge array passed as $excluded with current excluded property. If you want to override this default behaviour then you can set parameter $clean to be true.

ChromeHeadless::url('https://example.com')
                ->setExcluded([
                    'document'
                ])
                ->setExcluded([
                    'stylesheet',
                    'image'
                ]) // property excluded now will only have document and those two
                ->getDOMCrawler();
ChromeHeadless::url('https://example.com')
                ->setExcluded([
                    'stylesheet'
                    'image'
                ]) 
                ->setExcluded([
                    'document'
                ], true) // property excluded now will only have only document
                ->getDOMCrawler();

Viewport

You can specify a custom viewport that will be used when you make a request. By default the Chrome Headless standard of 800x600px will be used.

ChromeHeadless::url('https://example.com')
                ->setViewport([
                    'width' => 1920,
                    'height' => 1080
                ])
                ->getDOMCrawler();

Testing

You can run the tests by using

composer test

milivojsa/chrome-php 适用场景与选型建议

milivojsa/chrome-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 400 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dom」 「headless」 「webpage」 「chrome」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 milivojsa/chrome-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 milivojsa/chrome-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 milivojsa/chrome-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 400
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-04