PHP来执行casperjs / phantomjs脚本

I'm having trouble using PHP to execute a casperjs script:

<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
var_dump(exec("echo \$PATH"));
exec("/usr/local/bin/casperjs hello.js website.com 2>&1",$output);
var_dump($output);

Which results in the following output:

string(43) "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."
array(1) {
  [0]=>
  string(36) "env: node: No such file or directory"
}

The only stackoverflow posts I could find hinted that there's a problem with my paths, and that maybe the PHP user can't access what it needs.

I have also tried the following: sudo ln -s /usr/bin/nodejs /usr/bin/node

Does anyone know what I would need to do or change for this error to resolve?

Thanks

My guess is you have something, somewhere, that assumes node is installed.

First, are you running php from the commandline? I.e. as php test.php in a bash shell. If so, you can run the commands, below, as they are. If through a web server the environment can be different. I'd start with making a phpinfo(); script, and then run the troubleshooting commands through shell_exec() commands. But, as that is a pain, I'd get it working from the commandline first, and only mess around with this if the behaviour is different when run through a web server. (BTW, if you are running from a cron job, again, the environment can be slightly different. But only worry about this if it works from commandline but does not work from cron.)

Troubleshoot hello.js

The easy one. Make sure your script does not refer to node anywhere. Also remember you cannot use node modules. So look for require() commands that should not be there.

Troubleshoot your bash shell

Run printenv | grep -i node to see if anything is there. But when PHP runs a shell command, some other files get run too. So check what is in /etc/profile and ~/.bash_profile . Also check /etc/profile.d/, /etc/bashrc and ~/.bashrc. You're basically looking for anything that mentions node.

Troubleshoot phantomjs/casperjs

How did you install phantomjs and casperjs? Are the actual binaries under /usr/local/bin, or symlinks, or are they bash scripts to the . E.g. on my machine:

cd /usr/local/bin
ls -l casperjs phantomjs

gives:

lrwxrwxrwx 1 darren darren 36 Apr 29  2014 casperjs -> /usr/local/src/casperjs/bin/casperjs
lrwxrwxrwx 1 darren darren 57 Apr 29  2014 phantomjs -> /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

And then to check each file:

head /usr/local/src/casperjs/bin/casperjs
head /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs

The first tells me casper is actually a python script #!/usr/bin/env python, while the second fills the screen with junk, telling me it is a binary executable.