从php脚本运行时找不到phantomjs主机


phantomjs host not found when run from php script

我正试图从php运行rasterize.js脚本,以便在我这边生成页面的pdf。我对它进行了轻微的修改,以提供更好的错误消息。

在PHP中,我得到了以下代码:

exec($cmd.' '.$script.' '.$url.' '.$filename.' 0.8 '.$domain.' '.$session_id);

运行的完整命令如下:

/var/www/site/scripts/phantomjs /var/www/site/scripts/rasterize.js http://domain.site.com/index.php/pdf /var/www/pdfs/sample.pdf 0.8 domain.site.com ftrs44g1esnpjerqcube8bban1

如果我从命令行运行完全相同的代码,PDF就会被创建,但如果我从php运行它,我会收到错误(从rasterize.js生成)"Host domain.site.com not found"

有什么想法可能导致这个问题吗?

光栅化.js

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;
phantom.addCookie({
    'name':     'laravel_session',   /* required property */
    'value':    system.args[5],  /* required property */
    'domain':   system.args[4],           /* required property */
    'path' :'/'
});
page.onResourceError = function(resourceError) {
    page.reason = resourceError.errorString;
    page.reason_url = resourceError.url;
};
if (system.args.length < 3 || system.args.length > 6) {
    console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
    console.log('  paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 630, height: 891 };
    page.paperSize = { format: 'A4', orientation: 'portrait', margin: '1cm' };
    if (system.args.length > 3) {
        page.zoomFactor = system.args[3];
    }
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log("Error opening url '"" + page.reason_url
                        + "'": " + page.reason);
            phantom.exit();
        } else {
            window.setTimeout(function () {
                page.render(output);
                phantom.exit();
            }, 200);
        }
    });
}

问题是VPS在代理后面。使用PHP中的exec并没有复制环境。

仅对http_proxy和https_proxy变量使用putenv就解决了这个问题。