我使用的是Google无头浏览器wrapper for PHP,并且试图使浏览器具有持久性,例如in example。
但是。 每次启动脚本时,浏览器都会从头开始初始化,始终无法连接到套接字。
$socketFile = '/tmp/debug/chrome-php-demo-socket1';
$browser = null;
if (file_exists($socketFile)) {
$socket = file_get_contents($socketFile);
try {
$browser = \HeadlessChromium\BrowserFactory::connectToBrowser($socket, [
'debugLogger' => 'php://stdout'
]);
} catch (\HeadlessChromium\Exception\BrowserConnectionFailed $e) {
echo 'browser was closed';
}
}
if (!$browser) {
$factory = new \HeadlessChromium\BrowserFactory('/usr/bin/google-chrome');
$browser = $factory->createBrowser([
'headless' => false,
'userDataDir' => '/tmp/debug/users/test',
'debugLogger' => 'php://stdout',
'keepAlive' => true
]);
file_put_contents($socketFile, $browser->getSocketUri());
}
$page = $browser->createPage();
$page->navigate('http://example.com')->waitForNavigation();
套接字文件本身存在。
stat /tmp/debug/chrome-php-demo-socket1
File: /tmp/debug/chrome-php-demo-socket1
Size: 74 Blocks: 8 Block В/В: 4096 regular file
...而且,无论如何,连接失败:
[2019-11-19 13:24:09] DEBUG Browser Factory: connecting using ws://127.0.0.1:41051/devtools/browser/aeb4d701-0677-44c0-a005-18933018ce04
[2019-11-19 13:24:09] DEBUG socket(1): connecting
[2019-11-19 13:24:09] DEBUG socket(1): ✗ could not connect
browser was closed
...
[2019-11-19 13:24:09] DEBUG process: initializing
该异常发生在/vendor/wrench/wrench/lib/Wrench/Client.php中 它说:
Could not connect to socket: Connection refused (111)
请指导我解决此问题。