[英]phantomjs host not found when run from php script
I'm trying to run the rasterize.js script from php in order to generate pdfs of a page on my side. 我正在尝试从php运行rasterize.js脚本,以便生成我这边的页面pdf。 I've modified it slightly to give better error messages. 我已对其进行了轻微修改,以提供更好的错误消息。
In PHP I've got the following code: 在PHP中,我有以下代码:
exec($cmd.' '.$script.' '.$url.' '.$filename.' 0.8 '.$domain.' '.$session_id);
The full command that runs looks like this: 运行的完整命令如下所示:
/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
If I run the exact same code from the command line, the PDF is created but if I run it from php I get the error (generated from rasterize.js) " Host domain.site.com not found" . 如果从命令行运行完全相同的代码,则会创建PDF,但是如果从php运行,则会收到错误消息(从rasterize.js生成)“ 找不到host domain.site.com” 。
Any ideas on what might be causing the issue? 关于什么可能导致问题的任何想法?
rasterize.js rasterize.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);
}
});
}
The issue was the VPS was behind a proxy. 问题是VPS在代理后面。 Using exec from PHP didn't copy the environment. 从PHP使用exec不会复制环境。
Just using putenv for the http_proxy and https_proxy variables solved it. 仅将putenv用于http_proxy和https_proxy变量即可解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.