# @extractr-io/puphpeteer

> A Puppeteer bridge for PHP, supporting the full API.

Latest version **0.2.2** (published 2018-04-20) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @extractr-io/puphpeteer
pnpm add @extractr-io/puphpeteer
yarn add @extractr-io/puphpeteer
bun add @extractr-io/puphpeteer
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2018-04-20 |
| First published | 2018-01-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 2 |
| Unpacked size | 28.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1325 |
| Author | Johann Pardanaud |
| Maintainers | nesk |
| Keywords | php, puppeteer, headless-chrome, testing, web, developer-tools, automation |

## Links

- npm: https://www.npmjs.com/package/@extractr-io/puphpeteer
- Repository: https://github.com/extractr-io/puphpeteer
- Homepage: https://github.com/extractr-io/puphpeteer#readme
- Issues: https://github.com/extractr-io/puphpeteer/issues
- npm.io page: https://npm.io/package/@extractr-io/puphpeteer

## Dependencies (2)

- [puppeteer](https://npm.io/package/puppeteer.md) ~1.3.0
- [@extractr-io/rialto](https://npm.io/package/@extractr-io/rialto.md) ~0.1.2

## Alternatives

- [@snazzah/davey](https://npm.io/package/@snazzah/davey.md) — 1.5M weekly downloads
- [@vendure/testing](https://npm.io/package/@vendure/testing.md) — 8.3K weekly downloads
- [vue-simple-context-menu](https://npm.io/package/vue-simple-context-menu.md) — 6.6K weekly downloads
- [cypress-webpack-preprocessor-v5](https://npm.io/package/cypress-webpack-preprocessor-v5.md) — 2.1K weekly downloads
- [@backstage/plugin-catalog-backend-module-puppetdb](https://npm.io/package/@backstage/plugin-catalog-backend-module-puppetdb.md) — 1.3K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2018-04-20
- 0.2.1 — 2018-04-09
- 0.2.0 — 2018-02-19
- 0.1.0 — 2018-01-29

## README

# PuPHPeteer

[![PHP Version](https://img.shields.io/packagist/php-v/extractr-io/puphpeteer.svg?style=flat-square)](http://php.net/)
[![Composer Version](https://img.shields.io/packagist/v/extractr-io/puphpeteer.svg?style=flat-square&label=Composer)](https://packagist.org/packages/extractr-io/puphpeteer)
[![Node Version](https://img.shields.io/node/v/@extractr-io/puphpeteer.svg?style=flat-square&label=Node)](https://nodejs.org/)
[![NPM Version](https://img.shields.io/npm/v/@extractr-io/puphpeteer.svg?style=flat-square&label=NPM)](https://www.npmjs.com/package/@extractr-io/puphpeteer)
[![Build Status](https://img.shields.io/travis/extractr-io/puphpeteer.svg?style=flat-square&label=Build%20Status)](https://travis-ci.org/extractr-io/puphpeteer)

A [Puppeteer](https://github.com/GoogleChrome/puppeteer/) bridge for PHP, supporting the full API. Based on [Rialto](https://github.com/extractr-io/rialto/), a package to manage Node resources from PHP.

Here are some examples [borrowed from Puppeteer's documentation](https://github.com/GoogleChrome/puppeteer/blob/master/README.md#usage) and adapted to PHP's syntax:

**Example** - navigating to https://example.com and saving a screenshot as *example.png*:

```php
use ExtractrIo\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:

```php
use ExtractrIo\Puphpeteer\Puppeteer;
use ExtractrIo\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::create("
    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.1 and Node >= 8.

Install it with these two command lines:

```shell
composer require extractr-io/puphpeteer
npm install @extractr-io/puphpeteer
```

## Notable differences between PuPHPeteer and Puppeteer

### Puppeteer's class must be instanciated

Instead of requiring Puppeteer:

```js
const puppeteer = require('puppeteer');
```

You have to instanciate the `Puppeteer` class:

```php
$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](https://github.com/extractr-io/rialto/blob/master/docs/api.md#options).

**Note:** If you use some timeouts higher than 30 seconds in Puppeteer's API, you will have to set a higher value for the `read_timeout` option (default: `35`):

```php
$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:

```php
$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](https://github.com/extractr-io/rialto/blob/master/docs/api.md#javascript-functions), the body of these functions must be written in JavaScript instead of PHP.

```php
use ExtractrIo\Rialto\Data\JsFunction;

$pageFunction = JsFunction::create(['element'], "
    return element.textContent;
");
```

### Exceptions must be catched 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`:

```php
use ExtractrIo\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](LICENSE) for more information.

---
_Source: https://npm.io/package/@extractr-io/puphpeteer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
