I'm trying to run a PHP code in isolation. The php code simply calls a node script at intervals with required arguments. The Node.js script on the other hand computes the arguments to get additional data from mongoDb and then spawns a child process within it to call Phantomjs with those arguments and creates heatmap image file(s).
Now the Node.js script runs fine when fired from command line with the right arguments.
But no matter what I try I'm unable to get it to work when called from php.
I have tried shell_exec and exec like below:
$node = '/usr/local/bin/node';
$scriptPath = $this->basePath . '/web/scripts/node/heatmaps_node_wrapper.js';
$clickmapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' click';
$mousemovemapCmd = $node . ' ' . $scriptPath . ' ' . $url . ' ' . $groupId . ' ' . $heatmapId . ' mousemove';
$asyncCmd = ' >' . $this->basePath . '/logs/heatmap.io.log 2>'. $this->basePath . '/logs/heatmap.err.log &';
exec($clickmapCmd . $asyncCmd);
exec($mousemovemapCmd . $asyncCmd);
also tried using symfony's ProcessBuilder Component, like so -
$builder = new ProcessBuilder();
$builder->setPrefix('/usr/local/bin/node');
$builder->setArguments(array($scriptPath, $url, $groupId, $heatmapId, 'click'));
$process = $builder->getProcess();
$process->setTimeout(1000);
$process->setIdleTimeout(1000);
$process->start();
$output = $process->getOutput();
Both fail to create the heatmap images.
But debugging the symfony code and studying symfony's Process object helped me discover the likely problem. In the objects stderr I see the following message -
events.js:85
throw er; // Unhandled 'error' event
^
Error: spawn phantomjs ENOENT
at exports._errnoException (util.js:746:11)
at Process.ChildProcess._handle.onexit (child_process.js:1053:32)
at child_process.js:1144:20
at process._tickCallback (node.js:355:11)
So my guess is the problem is with Node trying to create a child process of, of PHP's child process(??).
So my question is, what is the solution (or work around) to this problem??
Cheers!
The problem is that node can't find phantomjs
. If it's not in the $PATH
for the user executing the node script, you may try using the full path to the phantomjs
binary. You may also want to check the executable permissions on the phantomjs
binary.