-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconnect_external_browser.php
More file actions
65 lines (52 loc) · 2.39 KB
/
connect_external_browser.php
File metadata and controls
65 lines (52 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require __DIR__.'/../../vendor/autoload.php';
use Playwright\Exception\PlaywrightException;
use Playwright\PlaywrightFactory;
$playwright = PlaywrightFactory::create();
// Example 1: launchServer() exposes a Playwright WebSocket endpoint that can be reused
// by other processes. This endpoint is ephemeral and valid while the server is running.
echo "=== Example 1: Launch Browser Server ===\n";
$server = $playwright->launchServer('chromium', ['headless' => true]);
$wsEndpoint = $server->wsEndpoint();
echo "Browser server started at: {$wsEndpoint}\n";
// Example 2: connect() uses Playwright protocol, so it must target a launchServer() endpoint.
echo "\n=== Example 2: Connect to Browser Server ===\n";
$browser = $playwright->chromium()->connect($wsEndpoint);
$context = $browser->newContext();
$page = $context->newPage();
$page->goto('https://example.com');
echo "Page title: {$page->title()}\n";
$browser->close();
// Closing the client connection does not stop the server process.
$server->close();
// Example 3: connectOverCDP() is Chromium-only and attaches to a running Chrome instance.
// First, start Chrome with remote debugging enabled.
echo "\n=== Example 3: Connect Over CDP (Chromium only) ===\n";
echo "To use this example:\n";
echo "1. Start Chrome with remote debugging on port 9222\n";
echo " macOS example:\n";
echo " \"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\" --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-pw\n";
echo "2. Optionally set CDP_ENDPOINT (default: http://127.0.0.1:9222)\n";
$cdpEndpoint = getenv('CDP_ENDPOINT') ?: 'http://127.0.0.1:9222';
try {
$cdpBrowser = $playwright->chromium()->connectOverCDP($cdpEndpoint);
$cdpContext = $cdpBrowser->newContext();
$cdpPage = $cdpContext->newPage();
$cdpPage->goto('https://playwright.dev');
echo "Connected via CDP! Title: {$cdpPage->title()}\n";
$cdpBrowser->close();
} catch (PlaywrightException $e) {
echo "CDP connection failed at {$cdpEndpoint}: {$e->getMessage()}\n";
}
$playwright->close();
echo "\nDone!\n";