问题
the following works fine on the command line for user xyz:
/opt/local/bin/phantomjs --version
1.9.1
But, in PHP, with the same user (xyz), I get an error code 5
exec('/opt/local/bin/phantomjs --version', $o, $r)
echo $r
I'm using MAMP, and exec() normally works fine. If anyone knows how to debug this I would e very greatful!
回答1:
Thanks to Amal Murali for pointing me in the right direction!
This is documented also in exec() command on mac osx returns 5, and can be fixed by unsetting DYLD_LIBRARY_PATH.
The following command will work:
exec('unset DYLD_LIBRARY_PATH ; /opt/local/bin/phantomjs --version', $o, $r)
echo $r
Cheers,
Albert.
回答2:
Redirect the output by appending 2>&1
at the end of your command, like so:
exec('/opt/local/bin/phantomjs --version 2>&1', $o, $r);
var_dump($r);
The 2>&1
says to send standard error to where ever standard output is being redirected as well. See this answer for more information.
回答3:
It is likely a problem with the environment variables. A common issue with macports is using the 'stock' apachectl that comes with OSX (located at /usr/sbin/apachectl) instead of the macports apachectl located at /opt/local/apache2/bin/apachectl.
In most cases, the stock version will appear to work correctly. However, only the macports version will establish the correct environment (i.e,. using /opt/local/apache2/bin/envvars, and setting DYLD_FALLBACK_LIBRARY_PATH correctly.
If you run sudo apachectl
to control apache, you are likely running the stock version of apachectl.
You may want to delete it (or rename it to /usr/sbin/apachectl.orig) and symlink instead to the macports version:
mv /usr/bin/apachectlapachectl /usr/bin/apachectlapachectl.orig
ln /opt/local/apache2/bin/apachectl /usr/bin/apachectl
If you are still having issues then you may need to delve deepet into the environment variables being used by apache (and thus passed to php).
来源:https://stackoverflow.com/questions/19008960/phantomjs-on-mac-os-x-works-from-the-command-line-not-via-exec