All Projects → rialto-php → Puphpeteer

rialto-php / Puphpeteer

Licence: mit
A Puppeteer bridge for PHP, supporting the entire API.

Projects that are alternatives of or similar to Puphpeteer

Phantomas
Headless Chromium-based web performance metrics collector and monitoring tool
Stars: ✭ 2,191 (+116.07%)
Mutual labels:  automation, developer-tools, puppeteer, headless-chrome
Puppeteer Examples
Puppeteer example scripts for running Headless Chrome from Node.
Stars: ✭ 2,781 (+174.26%)
Mutual labels:  developer-tools, puppeteer, headless-chrome
Puppeteer
Headless Chrome Node.js API
Stars: ✭ 75,197 (+7315.88%)
Mutual labels:  automation, developer-tools, headless-chrome
Ferrum
Headless Chrome Ruby API
Stars: ✭ 1,009 (-0.49%)
Mutual labels:  automation, developer-tools, headless-chrome
Deno Puppeteer
A port of puppeteer running on Deno
Stars: ✭ 128 (-87.38%)
Mutual labels:  automation, puppeteer, headless-chrome
Puppeteer Api Zh cn
📖 Puppeteer中文文档(官方指定的中文文档)
Stars: ✭ 697 (-31.26%)
Mutual labels:  automation, developer-tools, puppeteer
Awesome Puppeteer
A curated list of awesome puppeteer resources.
Stars: ✭ 1,728 (+70.41%)
Mutual labels:  automation, puppeteer, headless-chrome
Puppeteer Extra
💯 Teach puppeteer new tricks through plugins.
Stars: ✭ 3,397 (+235.01%)
Mutual labels:  automation, puppeteer, headless-chrome
Apify Js
Apify SDK — The scalable web scraping and crawling library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.
Stars: ✭ 3,154 (+211.05%)
Mutual labels:  automation, puppeteer, headless-chrome
Md To Pdf
Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
Stars: ✭ 374 (-63.12%)
Mutual labels:  puppeteer, headless-chrome
Pptraas.com
Puppeteer as a service
Stars: ✭ 433 (-57.3%)
Mutual labels:  puppeteer, headless-chrome
Headless Chrome Crawler
Distributed crawler powered by Headless Chrome
Stars: ✭ 5,129 (+405.82%)
Mutual labels:  puppeteer, headless-chrome
Webster
a reliable high-level web crawling & scraping framework for Node.js.
Stars: ✭ 364 (-64.1%)
Mutual labels:  puppeteer, headless-chrome
Pyppeteer
Headless chrome/chromium automation library (unofficial port of puppeteer)
Stars: ✭ 3,480 (+243.2%)
Mutual labels:  puppeteer, headless-chrome
Nickjs
Web scraping library made by the Phantombuster team. Modern, simple & works on all websites. (Deprecated)
Stars: ✭ 494 (-51.28%)
Mutual labels:  automation, headless-chrome
Mochify.js
☕️ TDD with Browserify, Mocha, Headless Chrome and WebDriver
Stars: ✭ 338 (-66.67%)
Mutual labels:  puppeteer, headless-chrome
Budibase
Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
Stars: ✭ 8,071 (+695.96%)
Mutual labels:  automation, developer-tools
Puppeteer Lambda Starter Kit
Starter Kit for running Headless-Chrome by Puppeteer on AWS Lambda.
Stars: ✭ 563 (-44.48%)
Mutual labels:  puppeteer, headless-chrome
Differencify
Differencify is a library for visual regression testing
Stars: ✭ 572 (-43.59%)
Mutual labels:  puppeteer, headless-chrome
Rendertron
A Headless Chrome rendering solution
Stars: ✭ 5,593 (+451.58%)
Mutual labels:  puppeteer, headless-chrome

PuPHPeteer

PHP Version Composer Version Node Version NPM Version Build Status

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 nesk/puphpeteer
npm install @nesk/puphpeteer

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
  • $x => querySelectorXPath
  • $eval => querySelectorEval
  • $$eval => querySelectorAllEval

Use these aliases just like you would have used the original methods:

$divs = $page->querySelectorAll('div');

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.

License

The MIT License (MIT). Please see License File for more information.

Logo attribution

PuPHPeteer's logo is composed of:

Thanks to Laravel News for picking the icons and colors of the logo.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].