和phantomjs问题带有绝对路径

和phantomjs问题带有绝对路径

本文介绍了exec()和phantomjs问题带有绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using phantomJS for the purposes of programmatically taking screenshots of a webpage. My webserver runs on Linux 64 bit.

我的test.php文件

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

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

I open test.php in a browser. The out put I get is:

1.9.1 // version number
0 // exit code

这证明我可以通过exec()运行命令,并且phantomJS运行正常.

This proves that I can run commands through exec() and phantomJS is working perfectly.

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

Now when I replace the above code with:

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

但结果是相同的(段错误)

but the result was the same (segfault)

test.js 代码:

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

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

This makes me believe that using the full path as the second argument of phantomJS is causing it to crash. Thus, the things that I'm wondering are:

  • 我的假设对吗?
  • 还是因为我的Web服务器受到某些限制而阻止了exec()通过绝对URL访问.js文件?
  • Am I right in my assumption?
  • Or is it because of some restriction on my webserver which is blocking exec() from accessing the .js file through absolute URL?

推荐答案

经过大量搜索和测试,我发现它可以与以下新增功能一起使用:

After a lot of searching and testing I got it to work with following additions:

//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.是的.

I'm not the best server admin, so I can not explain everything in detail, but the lines above generate an pdf. Yeah.

这篇关于exec()和phantomjs问题带有绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:20