chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.2k stars 269 forks source link

fix guessChromeBinaryPath on Ubuntu Linux + add Chromium #595

Closed divinity76 closed 5 months ago

divinity76 commented 5 months ago

on Ubutu Linux, "command" is a bash builtin, not a standalone binary, and shell_exec() tries to find the "command" binary and fails, thus the original code never worked on Ubuntu Linux (I think the same is true for the entire Debian-derived family of operating systems, but don't quote me on that), fixing that by doing bash -c 'command... instead.

Also added chromium support, for those who have Chromium but not Chrome installed.

It will look for Chrome first, and if it cannot find Chrome, it will look for Chromium second, and if all fails, it falls back to hardcoded "chrome".

enricodias commented 5 months ago

I use Ubuntu and it works fine here 🤔

λ command -v google-chrome
/usr/bin/google-chrome
divinity76 commented 5 months ago

I use Ubuntu and it works fine here 🤔

λ command -v google-chrome
/usr/bin/google-chrome

yeah in your shell with your shell builtins, but try

php -r 'var_dump(shell_exec("command -v google-chrome"));'

and then try

php -r 'var_dump(shell_exec("bash -c '\''command -v google-chrome'\''"));'
enricodias commented 5 months ago

Same thing in both:

λ php -r 'var_dump(shell_exec("command -v google-chrome"));'  
string(23) "/usr/bin/google-chrome
"
λ php -r 'var_dump(shell_exec("bash -c '\''command -v google-chrome'\''"));'  
string(23) "/usr/bin/google-chrome
"
divinity76 commented 5 months ago

@enricodias well that confuses me, what do you get by

type command;
php -r 'var_dump(shell_exec("which command"));'

?

When I do it, I get

hans@DESKTOP-EE15SLU:/$ type command; php -r 'var_dump(shell_exec("which command"));'
command is a shell builtin
NULL
enricodias commented 5 months ago

I got the same;

command is a shell builtin
NULL

It is a builtin, but shell_exec can still use it.

divinity76 commented 5 months ago

@enricodias wow.. you're entirely correct!

I could've sworn that php -r 'var_dump(shell_exec("command -v google-chrome"));' didn't work for me yesterday, and concluded that shell_exec() doesn't have access to shell builtins, ... but it does work now! nevermind 🤷‍♂️ and sorry