7

我正在使用phantomJS以编程方式截取网页的屏幕截图。我的网络服务器在 64 位 Linux 上运行。

情景

我的test.php档案

exec('./phantomjs --version', $o, $e);
print_r($o);
echo $e;

test.php在浏览器中打开。我得到的输出是:

1.9.1 // version number
0 // exit code

这证明我可以运行命令exec()并且运行phantomJS良好。

问题

现在,当我将上面的代码替换为:

exec('./phantomjs http://mywebsite.com/test.js', $o, $e);
print_r($o);
echo $e;

输出是:

Array ( ) // empty output
139 // exit code which on investigating turned out to be segmentation fault

我也试过:

exec('./phantomjs ./test.js', $o, $e); // since phantomjs and test.js are in same folder

但结果是一样的(段错误)

test.js代码:

var page = require('webpage').create();
var url = 'http://www.rediff.com/';
page.open(url, function (status) {
    phantom.exit();
});

这让我相信使用完整路径作为第二个参数phantomJS会导致它崩溃。因此,我想知道的是:

  • 我的假设是对的吗?
  • 还是因为对我的网络服务器的某些限制阻止了通过绝对 URLexec()访问文件?.js
4

4 回答 4

3

经过大量搜索和测试后,我得到了以下添加:

//throws a lot of errors because searching some libraries
$cmd = 'unset DYLD_LIBRARY_PATH ;';
$cmd.= ' /abs/path/to/phantomjs';
$cmd.= ' /abs/path/to/script.js';

//set environment variable to node source
putenv('PATH=/abs/path/to/node/bin/');

//now exec the cmd and pipe the errors to stdout
exec($cmd.' 2>&1', $output);

//and output the results
print_r($output);

我不是最好的服务器管理员,所以我无法详细解释所有内容,但上面的行会生成一个 pdf。是的。

于 2014-02-20T17:42:09.110 回答
1

我有一个类似的问题。PHP + PhantomJS Rasterize 我发现 phantomjs 不喜欢从 apache 进程运行。尝试从命令行运行 exec 命令:

php -r "exec('./phantomjs http://mywebsite.com/test.js', $o, $e); print_r($o); echo $e;"

如果这可行,您有几个选择:

1.) 有些人建议修改 sudoers 为 apache 用户提供 phantomjs 二进制文件的无密码 sudo 权限

2.) 像我一样,将你的脚本作为 cron 运行。

于 2014-01-28T21:47:57.153 回答
0

尝试放置test.js在文件夹中test.php(调用时exec('./phantomjs ./test.js', $o, $e);)或使用完整路径。

于 2013-10-04T18:42:04.710 回答
0

我发现问题出在 selinux 上(现在默认情况下在所有生产服务器构建中都已禁用它作为标准)。

在文件 /etc/selinux/config 中找到包含以下内容的行:

SELINUX=

并将其更改为:

SELINUX=禁用

运行命令(无需重启即可立即生效)

/usr/sbin/setenforce 0

于 2015-12-04T02:50:43.207 回答