zoon/puphpeteer
Composer 安装命令:
composer require --dev zoon/puphpeteer
包简介
A Puppeteer bridge for PHP, supporting the entire API.
README 文档
README
A Puppeteer bridge for PHP, supporting the entire API. Based on Rialto, a package to manage Node resources from PHP.
Here are some examples borrowed from Puppeteer's documentation and adapted to PHP's syntax:
Example - navigating to https://example.com and saving a screenshot as example.png:
use Nesk\Puphpeteer\Puppeteer; $puppeteer = new Puppeteer; $browser = $puppeteer->launch(); $page = $browser->newPage(); $page->goto('https://example.com'); $page->screenshot(['path' => 'example.png']); $browser->close();
Example - evaluate a script in the context of the page:
use Nesk\Puphpeteer\Puppeteer; use Nesk\Rialto\Data\JsFunction; $puppeteer = new Puppeteer; $browser = $puppeteer->launch(); $page = $browser->newPage(); $page->goto('https://example.com'); // Get the "viewport" of the page, as reported by the page. $dimensions = $page->evaluate(JsFunction::createWithBody(" return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, deviceScaleFactor: window.devicePixelRatio }; ")); printf('Dimensions: %s', print_r($dimensions, true)); $browser->close();
Requirements and installation
This package requires PHP >= 7.3 and Node >= 8.
Install it with these two command lines:
composer require zoon/puphpeteer npm install git+https://git@github.com/zoonru/puphpeteer.git#zoon
Use with browserless
docker run --rm -p 3000:3000 ghcr.io/browserless/chrome
$puppeteer = new Nesk\Puphpeteer\Puppeteer; $options = [ 'headless' => false, 'stealth'=> true, 'timeout'=> 5000, 'args' => [ '--window-size=1366,768', ], ]; $browser = $puppeteer->connect(['browserWSEndpoint' => 'ws://127.0.0.1:3000/chrome?launch='.urlencode(json_encode($options, JSON_UNESCAPED_UNICODE))]); $page = $browser->newPage(); $page->goto('https://www.example.com'); $page->screenshot(['path' => 'example.png']); $browser->close();
Notable differences between PuPHPeteer and Puppeteer
Puppeteer's class must be instantiated
Instead of requiring Puppeteer:
const puppeteer = require('puppeteer');
You have to instantiate the Puppeteer class:
$puppeteer = new Puppeteer;
This will create a new Node process controlled by PHP.
You can also pass some options to the constructor, see Rialto's documentation. PuPHPeteer also extends these options:
[
// Logs the output of Browser's console methods (console.log, console.debug, etc...) to the PHP logger
'log_browser_console' => false,
]
⏱ Want to use some timeouts higher than 30 seconds in Puppeteer's API?
If you use some timeouts higher than 30 seconds, you will have to set a higher value for the read_timeout option (default: 35):
$puppeteer = new Puppeteer([ 'read_timeout' => 65, // In seconds ]); $puppeteer->launch()->newPage()->goto($url, [ 'timeout' => 60000, // In milliseconds ]);
No need to use the await keyword
With PuPHPeteer, every method call or property getting/setting is synchronous.
Some methods have been aliased
The following methods have been aliased because PHP doesn't support the $ character in method names:
$=>querySelector$$=>querySelectorAll$eval=>querySelectorEval$$eval=>querySelectorAllEval
Use these aliases just like you would have used the original methods:
$divs = $page->querySelectorAll('div'); // Runs the `//h2` as the XPath expression. $xpath = $page->querySelectorAll('::-p-xpath(//h2)'); // div element that has Checkout as the inner text. $text = $page->querySelector('div ::-p-text(Checkout)');
Evaluated functions must be created with JsFunction
Functions evaluated in the context of the page must be written with the JsFunction class, the body of these functions must be written in JavaScript instead of PHP.
use Nesk\Rialto\Data\JsFunction; $pageFunction = JsFunction::createWithParameters(['element']) ->body("return element.textContent");
Exceptions must be caught with ->tryCatch
If an error occurs in Node, a Node\FatalException will be thrown and the process closed, you will have to create a new instance of Puppeteer.
To avoid that, you can ask Node to catch these errors by prepending your instruction with ->tryCatch:
use Nesk\Rialto\Exceptions\Node; try { $page->tryCatch->goto('invalid_url'); } catch (Node\Exception $exception) { // Handle the exception... }
Instead, a Node\Exception will be thrown, the Node process will stay alive and usable.
Puppeteer plugins
Puppeteer-extra and puppeteer-extra-plugin-stealth plugins already added in npm requirements.
To use them, override js inclusion with js_extra option
$puppeteer = new Puppeteer([ 'js_extra' => /** @lang JavaScript */ " const puppeteer = require('puppeteer-extra'); const StealthPlugin = require('puppeteer-extra-plugin-stealth'); puppeteer.use(StealthPlugin()); instruction.setDefaultResource(puppeteer); " ]);
License
The MIT License (MIT). Please see License File for more information.
Logo attribution
PuPHPeteer's logo is composed of:
- Puppet by Luis Prado from the Noun Project.
- Elephant by Lluisa Iborra from the Noun Project.
Thanks to Laravel News for picking the icons and colors of the logo.
zoon/puphpeteer 适用场景与选型建议
zoon/puphpeteer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 245.23k 次下载、GitHub Stars 达 209, 最近一次更新时间为 2023 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「web」 「automation」 「developer-tools」 「puppeteer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zoon/puphpeteer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zoon/puphpeteer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zoon/puphpeteer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
Model of a web-based resource
Retrieve a WebResourceInterface instance over HTTP
The PHP SDK for Checkmango
Selectize Theme for Bootstrap 4
Provides command to manage a web library directory
统计信息
- 总下载量: 245.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 209
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-17
