npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@popouille/puphpeteer

v1.0.6

Published

A Puppeteer bridge for PHP, supporting the entire API.

Downloads

10

Readme

PuPHPeteer

Laravel 10 Support

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 three command lines:

composer require popouille/puphpeteer
sudo npm install @popouille/puphpeteer --unsafe-perm=true --allow-root

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,
]

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.

Puppeteer plugins

To use puppeteer-extra plugins add them to your project:

npm install puppeteer puppeteer-extra puppeteer-extra-plugin-stealth

Then 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:

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