我正在使用 phantomJS为了以编程方式截取网页的屏幕截图。我的网络服务器在 Linux 64 位上运行。
场景
我的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
的第二个参数会导致它崩溃。因此,我想知道的是:
- 我的假设是否正确?
- 还是因为我的网络服务器上的某些限制阻止
exec()
通过绝对 URL 访问.js
文件?
最佳答案
经过大量搜索和测试后,我通过添加以下内容使其可以正常工作:
//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。是的。
关于php - exec() 和 phantomjs 问题与绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18672586/