PHP code example of aktuba / php-puppeteer

1. Go to this page and download the library: Download aktuba/php-puppeteer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aktuba / php-puppeteer example snippets


use PhpPupeeteer\PhpPupeeteer;

$phpPuppeteer = new PhpPupeeteer;
$browser = $phpPuppeteer->getBrowser([
    'args' => [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-gpu',
        '--incognito',
    ],
]);
$page = $browser->newPage();
$page->gotoWithWait('https://example.com');
$page->screenshot(['path' => 'example.png']);
$browser->close();

use PhpPupeeteer\PhpPupeeteer;

$phpPuppeteer = new PhpPupeeteer;
$browser = $phpPuppeteer->getBrowser([
    'headless' => false,
    'args' => [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--incognito',
        '--start-maximized',
    ],
]);
$page = $browser->newPage();
$page->gotoWithWait('https://example.com');
$page->screenshot(['path' => 'example.png']);
$browser->close();

use Puphpeteer\Puppeteer;
use Puphpeteer\Data\Js;

$puppeteer = new PhpPupeeteer;

$browser = $phpPuppeteer->getBrowser();
$page = $browser->newPage();
$page->gotoWithWait('https://example.com');

// Get the "viewport" of the page, as reported by the page.
$dimensions = $page->evaluate(Js::createWithBody("
    return {
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        deviceScaleFactor: window.devicePixelRatio,
    };
"));

printf('Dimensions: %s', print_r($dimensions, true));

$browser->close();

use PhpPupeeteer\PhpPupeeteer;

$puppeteer = PhpPupeeteer;

[
    // Logs the output of Browser's console methods (console.log, console.debug, etc...) to the PHP logger
    'log_browser_console' => false,
]

$phpPuppeteer = $phpPuppeteer->getBrowser([
	'read_timeout' => 65, // In seconds
]);

$browser->newPage()->goto($url, [
    'timeout' => 60000, // In milliseconds
]);

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

use PhpPupeeteer\Data\Js;

$pageFunction = Js::createWithParameters(['element'])
    ->body("return element.textContent")
;

use Nesk\Rialto\Exceptions\Node;

try {
    $page->tryCatch->goto('invalid_url');
} catch (Node\Exception $exception) {
    // Handle the exception...
}
shell
composer nesk/puphpeteer