Examples
How to take a full page screenshot with PHP and Selenium
Install php-webdriver and chromedriver
composer require php-webdriver/webdriver
chromedriver --port=9515 php-webdriver has no Selenium Manager, so it needs a running chromedriver matching your Chrome version. Start it in another terminal and leave it running while the script runs — the examples connect to port 9515, chromedriver’s default, and passing --port=9515 just makes that explicit.
Take full page screenshot and save to file
<?php
require __DIR__ . '/vendor/autoload.php';
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverDimension;
$options = new ChromeOptions();
$options->addArguments(['--headless=new']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);
$driver->get('https://www.htmltoimage.com/demo');
// Chrome can't natively capture beyond the viewport, so size the window
// to the full height of the document first.
$width = $driver->executeScript('return document.documentElement.scrollWidth');
$height = $driver->executeScript('return document.documentElement.scrollHeight');
$driver->manage()->window()->setSize(new WebDriverDimension($width, $height));
$driver->takeScreenshot('full-page.png');
$driver->quit(); Find out how to change the size of the viewport with PHP and Selenium.
That was 27 lines of code — plus a browser to run in production
Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:
curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com" Try the Urlbox screenshot API — free trial, no browser babysitting.