chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.25k stars 276 forks source link

Connect with external Headless Chrome container #630

Closed jeffreymoelands closed 3 months ago

jeffreymoelands commented 3 months ago

On our hosting enviroment we can spinup a Headless Chrome container. We get a url to connect with this container, is there a way we can use this library?

For example with de node package puppeteer you can connect with a browser url:

 const browser = await puppeteer.connect({browserURL: formattedURL});
enricodias commented 3 months ago

Presistent browser should work even if the uri points to another host.

jeffreymoelands commented 3 months ago

Presistent browser should work even if the uri points to another host.

Thx for your answer, probably I don't understand but even when I create a Persistent Browser I get the next error:

Unknown socket scheme: http

For example the url to the other host is: http://127.0.0.1:30000

enricodias commented 3 months ago

You need to use the chrome DevTools uri. It's a websocket address that starts with ws://. The chrome process gives you that uri in the output when starting with the flag --remote-debugging-port=0

jeffreymoelands commented 3 months ago

I found how to make it work:

            $client = new Client(['base_uri' => 'http://127.0.0.1:30000']);

            $response = $client->put('/json/new');
            $body = $response->getBody()->getContents();
            $data = json_decode($body, true);

            $webSocketDebuggerUrl = $data['webSocketDebuggerUrl'];
            $browser = \HeadlessChromium\BrowserFactory::connectToBrowser($webSocketDebuggerUrl, $options);

Thx for your help!

bmunslow commented 2 weeks ago

Hi @jeffreymoelands!

I'm experiencing the same Unknown socket scheme: http issue you reported in your previous comment. My hosting provider doesn't allow the ws:// scheme endpoint.

Could you please expand a bit further how you construct the Client object?

Your code sample doesn't seem to match the definition for the Wrench\Client class constructor used by this project (which expects a string URI as an argument, and not array).

Also, the Client class doesn't implement a put method.

Thanks in advance!

bmunslow commented 2 weeks ago

Could you please expand a bit further how you construct the Client object?

After examining the code closer, I was able to figure out you are using a Guzzle client, I was sidetracked by the class of the same name in chrome-php/wrench project.

In case someone else bumps into the same issue, this is an expanded example of how to make the solution work:

$client = new \GuzzleHttp\Client(['base_uri' => $uri]);

// It may be needed to include the Host header with the IP and port of the chrome service:
$request_options = [
  'headers' => [
    'Host' => "{$YOUR_IP}:{$YOUR_PORT}"
  ],
];

$response = $client->put('/json/new', $request_options);

$body = $response->getBody()->getContents();
$data = json_decode($body, true);

$webSocketDebuggerUrl = $data['webSocketDebuggerUrl'];
$browser = \HeadlessChromium\BrowserFactory::connectToBrowser($webSocketDebuggerUrl, $options);
jeffreymoelands commented 2 weeks ago

@bmunslow you are right, I used the Guzzle Client to get the webSocketDebuggerUrl. Sorry for my late response, I hadn't seen your comment yet.