No results from PHP exec of headless chrome
Posted by: arnb (---.dyn.optonline.net)
Date: March 21, 2025 08:52PM

Attempting to exec headless chrome from PHP to get the html source of a webpage after javascript runs, the command works from the windows command line, but returns nothing when executed in PHP. Note the url shown below is only for testing.

The command and script (used \ instead of / in exec below or it would not show on forum)

<?php
$output=null;
$retval=null;
exec('start chrome --headless --dump-dom https:\\developer.chrome.com\',$output, $retval);
var_dump($output);
var_dump($retval);
?>

Results
C:\wamp64\www\cz\Test\execchrome.php:5:
array (size=0)
empty
C:\wamp64\www\cz\Test\execchrome.php:6:int 0

I've also tried commands shell_exec and popen with similar results. There are no errors in the appache log. Any help appreciated.

Options: ReplyQuote
Re: No results from PHP exec of headless chrome
Posted by: Otomatic (Moderator)
Date: March 23, 2025 10:49AM

Don't just copy a code example, read the documentation, both PHP and chrome --headless.

You'll need to use shell_exec() or oblique quotes.
What's more, you need the full chrome path and the --disable-gpu option to make it work on Windows.

What works :
$command ='"C:\Program Files\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --dump-dom h ttps://developer.chrome.com';
$output = shell_exec($command);
if(is_null($output)) error_log("output is null" ) ;
elseif($output === false) error_log("output is false" ) ;
else error_log("output=".$output);
 
Note: delete the h ttps space to prevent it from becoming a link.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: No results from PHP exec of headless chrome
Posted by: arnb (---.dyn.optonline.net)
Date: March 23, 2025 09:36PM

Thank you, Thank you, Thank you! That works.

"Don't just copy a code example, read the documentation"
Well obviously I missed something. Not executing Chrome from its C: file location was the biggest issue, followed by not initially coding the --disable gpu parameter.

Arn B

Options: ReplyQuote


Sorry, only registered users may post in this forum.