Skip to content

Launching Chrome

Browser::launch() starts a browser process, waits for its DevTools endpoint, and connects to it. Pass a LaunchOptions instance to control which executable runs, where its profile lives, and which flags it receives.

launch.php
use Pam\Browser\Browser;
use Pam\Browser\Chromium\LaunchOptions;
$browser = Browser::launch(new LaunchOptions(
executable: null, // auto-detect, or an absolute path
userDataDirectory: null, // temporary profile, deleted on close
headless: true,
timeoutSeconds: 15.0, // how long to wait for Chrome to start
arguments: [], // extra command-line flags
));
Option Default Description
executable null Absolute path to Chrome or Chromium. When null, PAM_CHROME_PATH is used, then the first matching executable on PATH.
userDataDirectory null Profile directory. When null, a temporary pam-browser-* directory is created and removed on close.
headless true Adds --headless=new and --disable-gpu. Set to false to open a visible window.
timeoutSeconds 15.0 How long to wait for the DevTools endpoint. Must be greater than 0 and at most 300.
arguments [] Extra Chrome flags, appended after the defaults. Each must be non-empty and free of NUL bytes.

Every launch also passes --no-first-run, --no-default-browser-check, --disable-background-networking, --disable-component-update, and --disable-sync, and opens about:blank.

When executable is null, PAM Browser resolves the browser in this order:

  1. The PAM_CHROME_PATH environment variable.
  2. The first of google-chrome-stable, google-chrome, chromium, chromium-browser, or chrome found on PATH.

If nothing is found, launch() throws Pam\Browser\Exception\BrowserException with the message Chrome/Chromium was not found. Set PAM_CHROME_PATH to an executable path.

By default each launch gets a fresh, throwaway profile. Pass userDataDirectory to keep cookies, local storage, and logins between runs:

persistent.php
$browser = Browser::launch(new LaunchOptions(
userDataDirectory: __DIR__.'/storage/browser-profile',
));
echo $browser->profileDirectory(), PHP_EOL;

The directory is created with 0700 permissions if it does not exist. PAM Browser never deletes a directory you provide. Before launching, it removes a stale DevToolsActivePort marker left by a previous crash so it does not connect to an old port.

Use arguments for any Chrome switch you need, for example a fixed window size or a proxy:

$browser = Browser::launch(new LaunchOptions(arguments: [
'--window-size=1280,800',
'--proxy-server=http://proxy.example.com:3128',
'--lang=en-US',
]));

If your proxy needs credentials, see proxy authentication.

Set headless: false to watch what the browser does while you develop:

$browser = Browser::launch(new LaunchOptions(headless: false));

close() is idempotent. It sends Browser.close, closes the WebSocket, terminates the process if it is still running, and removes the temporary profile. Since 1.0.1, close() and the destructor never throw, even when the CDP connection is already gone, so the process is always stopped.

$browser = Browser::launch();
try {
// ...
} finally {
$browser->close();
}

If Chrome exits before it exposes its endpoint, launch() throws a BrowserException that includes Chrome’s exit code, and cleans up the temporary profile.