Skip to content

Raw CDP Access

Browser and Page cover the common tasks. When you need a CDP domain they do not wrap, such as Emulation, Network, or Input, drop down one level to Pam\Browser\Connection. It sends any command, returns its result as an array, and dispatches events to listeners.

The high-level Browser keeps its connection private, so for raw access you start the process and connect yourself. All the classes involved are public and stable.

raw-cdp.php
<?php
declare(strict_types=1);
require __DIR__.'/vendor/autoload.php';
use Pam\Browser\Chromium\BrowserProcess;
use Pam\Browser\Chromium\LaunchOptions;
use Pam\Browser\Connection;
use Pam\Browser\Page;
use Pam\Browser\Transport\WebSocketTransport;
$process = BrowserProcess::launch(new LaunchOptions());
try {
$connection = new Connection(WebSocketTransport::connect($process->webSocketUrl));
$target = $connection->command('Target.createTarget', ['url' => 'about:blank']);
$attached = $connection->command('Target.attachToTarget', [
'targetId' => $target['targetId'],
'flatten' => true,
]);
// Wrap the session in a Page to keep the high-level helpers.
$page = new Page($connection, $target['targetId'], $attached['sessionId']);
$page->enable();
$connection->command('Emulation.setDeviceMetricsOverride', [
'width' => 390,
'height' => 844,
'deviceScaleFactor' => 3,
'mobile' => true,
], $page->sessionId);
$page->navigate('https://example.com');
file_put_contents(__DIR__.'/mobile.png', $page->captureScreenshot());
$connection->close();
} finally {
$process->close();
}

BrowserProcess::launch() accepts the same LaunchOptions as Browser::launch() and exposes webSocketUrl and userDataDirectory. WebSocketTransport::connect() only accepts ws:// URLs, which is what a local Chrome exposes.

You can also connect to a Chrome you started yourself with --remote-debugging-port: read its browser WebSocket URL from http://127.0.0.1:<port>/json/version and pass it to WebSocketTransport::connect().

$result = $connection->command(
'Network.setExtraHTTPHeaders',
['headers' => ['X-Acme-Trace' => 'abc123']],
sessionId: $page->sessionId, // omit for browser-level commands
timeoutSeconds: 10.0, // default 30
);
  • Browser-level domains (Browser, Target) need no session id. Page-level domains need the page’s sessionId.
  • A protocol error throws Pam\Browser\Exception\CommandFailed, which carries commandId, protocolCode, and protocolMessage. For example, an unknown method gives code -32601.
  • A command without a response before the timeout throws ProtocolException.

Register listeners with on(). Each receives a Pam\Browser\Protocol\Event with method, parameters, and sessionId:

use Pam\Browser\Protocol\Event;
$connection->on('Runtime.consoleAPICalled', function (Event $event) use ($page): void {
if ($event->sessionId !== $page->sessionId) {
return;
}
echo 'console: ', $event->parameters['args'][0]['value'] ?? '', PHP_EOL;
});
$page->evaluate('console.log("hello from the page")');

Remember to enable the domain that produces the events (Runtime and Page are enabled by Page::enable(); others, like Network.enable, are up to you). Listeners run while the connection processes messages, including from inside another command, so a listener may itself send commands.

To block until a specific event arrives, use waitForEvent():

$connection->command('Page.reload', [], $page->sessionId);
$event = $connection->waitForEvent(
'Page.loadEventFired',
fn (Event $event): bool => $event->sessionId === $page->sessionId,
timeoutSeconds: 15.0,
);

Call $connection->pump($seconds) to process one incoming message when you are otherwise idle, exactly like Page::pump().