在我的服务器(Ubuntu 14.04.4 LTS)上,我安装了Firefox,以及用于无头Firefox操作的xvfb
以及带有SlimerJS的CasperJS。我也有一个CasperJS脚本,工作正常。我想从PHP使用这个脚本;这是我这个PHP脚本的精髓,让我们把它mytest.php
:在网络服务器上通过PHP运行SlimerJS +无头Firefox脚本?
echo "php_sapi_name() " . php_sapi_name() . "\n"; // "cli" for php cli, "apache2handler" for php via webserver
chdir(dirname(__FILE__));
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules";
putenv("SLIMERJSLAUNCHER=/usr/bin/firefox46");
$cmdline = "xvfb-run $nodeModPath/casperjs/bin/casperjs --engine=slimerjs --debug=true mySlimerScript.js";
$returnString = shell_exec($cmdline);
echo "$returnString\n";
编辑:请注意,命令可能也只是:
$cmdline = "xvfb-run $nodeModPath/casperjs/bin/casperjs --engine=slimerjs --debug=true 2>&1";
...这是,没有列出任何JS脚本 - 在这种情况下应该转储帮助(并且在CLI访问的情况下 - 但通过网络服务器访问时报告的错误与以下相同)
当我运行从终端命令行(通过SSH)这个PHP脚本,即通过PHP的CLI模式:
$ php mytest.php
...一切都正常运行,也没有任何问题。
然而,当我通过Web服务器在线调用此PHP脚本,即通过http://example.com/mytest.php
,它第一次失败,出现错误:
Gecko error: it seems /usr/bin/firefox46 is not compatible with SlimerJS.
See Gecko version compatibility. If version is correct, launch slimerjs
with --debug=true to see Firefox error message
...并添加--debug=true
后(如已包含在例如以上),我还收到此错误:
JavaScript error: resource://gre/modules/FileUtils.jsm, line 63: NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIProperties.get]
所以,显然我的无头Firefox不希望运行,当PHP通过Web服务器调用(在这种情况下,PHP报告说,它使用apache2handler
SAPI)。
有人会知道为什么会发生这种情况 - 以及如何在从Web服务器调用脚本时正确执行脚本,就像在PHP CLI模式下运行一样?编辑2:现在可以通过CLI模式重建此错误,并且可以确认它是由于用户;所以没有在$command
提供任何JS脚本,我得到这个:
$ sudo -H -u root php mytest.php
...
Usage: casperjs [options] script.[js|coffee] [script argument [script argument ...]]
casperjs [options] test [test path [test path ...]]
casperjs [options] selftest
...
$ sudo -H -u www-data php mytest.php
JavaScript error: resource://gre/modules/FileUtils.jsm, line 63: NS_ERROR_FAILURE: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIProperties.get]
Gecko error: it seems /usr/bin/firefox46 is not compatible with SlimerJS.
See Gecko version compatibility. If version is correct, launch slimerjs
with --debug=true to see Firefox error message
第一个想法:在'cli'模式下运行脚本的文件权限将让用户运行它,但是,通过网络服务器,用户将是'www-data'。可能,不是吗? –
的确,@ php-dev,但我无法真正弄清楚谁的权限会成为问题;这里我称之为'xvfb-run',它调用'casperjs',它调用调用无头Firefox的'slimerjs'。有没有办法在这种情况下检查权限错误?我尝试了'/ var/log/apache2/error-mysite.log',但它只报告了PHP的东西,例如''shell_exec'调用失败后试图获取非对象的属性。 – sdbbs
尝试将'stderr'输出重定向到'stdout'或文件。 –