如何解决错误QXcbConnection:使用PHP的phantomJs使用exec函数时无法连接显示

Sha*_*iya 5 php linux apache phantomjs ubuntu-16.04

我正在开发一个项目,我想在一起使用PHP和Phantomjs,我已经完成了我的phantomJs脚本并尝试使用php exec函数运行它.但该函数返回一个错误列表数组.下面我正在编写我的phantomjs和php代码

导游:/var/www/html/phantom/index.js

var page = require('webpage').create();
var fs = require('fs');

page.open('http://insttaorder.com/', function(status) {
    // Get all links to CSS and JS on the page
    var links = page.evaluate(function() {

        var urls = [];

        $("[rel=stylesheet]").each(function(i, css) {
            urls.push(css.href);
        });

        $("script").each(function(i, js) {
            if (js.src) {
                urls.push(js.src);
            }
        });

        return urls;
    });

    // Save all links to a file
    var url_file = "list.txt";
    fs.write(url_file, links.join("\n"), 'w');

    // Launch wget program to download all files from the list.txt to current
    // folder
    require("child_process").execFile("wget", [ "-i", url_file ], null,
            function(err, stdout, stderr) {

                console.log("execFileSTDOUT:", stdout);
                console.log("execFileSTDERR:", stderr);

                // After wget finished exit PhantomJS
                phantom.exit();

            });

});
Run Code Online (Sandbox Code Playgroud)

导游:/var/www/html/phantom/index.php

exec('/usr/bin/phantomjs  index.js 2>&1',$output);
echo '<pre>';
print_r($output);
die;
Run Code Online (Sandbox Code Playgroud)

也尝试过

 exec('/usr/bin/phantomjs  /var/www/html/phantom/index.js 2>&1',$output);
    echo '<pre>';
    print_r($output);
    die;
Run Code Online (Sandbox Code Playgroud)

运行后,我得到低于错误

Array
(
    [0] => QXcbConnection: Could not connect to display
    [1] => PhantomJS has crashed. Please read the bug reporting guide at
    [2] =>  and file a bug report.
    [3] => Aborted (core dumped)
)
Run Code Online (Sandbox Code Playgroud)

但是,如果我从终端运行index.php文件,如下所示:

user2@user2-H81M-S:/var/www/html/phantom$ php index.php
Run Code Online (Sandbox Code Playgroud)

然后它工作正常.我不知道如何解决它.请帮忙.

我正在使用以下版本

系统版本:Ubuntu 16.04.2 LTS
PHP版本:5.6
phantomJs版本:2.1.1

Jia*_*b77 8

您是否尝试在服务器上设置环境变量?或者在调用phantomjs之前添加它?

我遇到了同样的情况并找到了一些解决方案:

一个.将变量定义或设置QT_QPA_PLATFORMoffscreen:

QT_QPA_PLATFORM=offscreen /usr/bin/phantomjs index.js
Run Code Online (Sandbox Code Playgroud)

湾 或者将此行添加到您的.bashrc文件中(将其放在最后):

export QT_QPA_PLATFORM=offscreen
Run Code Online (Sandbox Code Playgroud)

C.或安装包xvfb,并调用xvfb-run之前phantomjs:

xvfb-run /usr/bin/phantomjs index.js
Run Code Online (Sandbox Code Playgroud)

d.或使用参数platform:

/usr/bin/phantomjs -platform offscreen index.js
Run Code Online (Sandbox Code Playgroud)

也许您不希望/不能在您的服务器上进行修改,在这种情况下,您可以尝试从官方网站下载静态二进制文件:

/path/to/the/bin/folder/phantomjs index.js
Run Code Online (Sandbox Code Playgroud)

和/或在.bash_aliases文件中创建一个别名,如下所示:

alias phantomjs=/path/to/the/bin/folder/phantomjs
Run Code Online (Sandbox Code Playgroud)

phantomjs如果您决定使用别名,请确保系统上尚未安装该功能.

如果文件.bash_aliases不存在,请随意创建它或alias.bashrc文件末尾添加行

一些参考: