2017-03-09 130 views
1
  • ubuntu16.04 64位
  • php7.0.15
  • phantomjs2.1.1

我使用下面的命令来运行phantomJS。phantomjs PHP执行sh文件


$result=exec("/usr/bin/phantomjs /var/Phantom/baidu.js 2>&1"); 
var_dump($result); 

baidu.js

var page = require('webpage').create(); 
page.viewportSize = { 
    width: 1366, 
    height: 768 
}; 
page.open('https://www.baidu.com/', function(status) { 
    console.log("Status: " + status); 
    if(status === "success") { 
    page.render('baidu.png'); 
    } 
    phantom.exit(); 
}); 

结果是string(21) "Aborted (core dumped)"


$cmd = 'unset DYLD_LIBRARY_PATH ;'; 
$cmd.= ' /usr/bin/phantomjs'; 
$cmd.= ' /var/Phantom/baidu.js'; 
$cmd='phantomjs -v'; 
exec($cmd.' 2>&1', $output); 
var_dump($output); 

输出是array(4) { [0]=> string(44) "QXcbConnection: Could not connect to display" [1]=> string(61) "PhantomJS has crashed. Please read the bug reporting guide at" [2]=> string(64) " and file a bug report." [3]=> string(21) "Aborted (core dumped)" }


$result2=exec("sh /var/Phantom/publish.sh 2>&1"); 
var_dump($result2); 

publish.sh

#!/bin/bash 
echo $PATH 
FILE_DIR="/var/data/Phantom" 
BIN_DIR="/usr/bin" 
DB_NAME="xxx" 
BCK_DIR="/var/data/Phantom" 

$BIN_DIR/phantomjs $FILE_DIR/baidu.js 
$BIN_DIR/mysqldump --opt -u$DB_USER -p$DB_PASS $DB_NAME > $BCK_DIR/$DB_NAME.sql 

的resuult2是string(84) "mysqldump: [Warning] Using a password on the command line interface can be insecure."(mysqldump的消息).Mysqldump已成功执行,但phantomjs失败。


  1. 我运行在终端的命令 “phantomjs baidu.js”,成功渲染图像。
  2. 我在终端中运行命令“sh publish.sh”,成功呈现图像。
  3. 所有有关权限是“777”

我发现有人已经successed从PHP the link执行phantomjs,但我不能执行它,所以这里是我的问题吗?

+0

我已经successed在exec中执行casperjs(“/ usr/local/bin/casperjs /var/www/test.js",$op,$er);'这里有http://casperjs.org/ –

+0

你能解决这个问题? – Vaviloff

+0

是的,它成功 –

回答

0

你其实分别是能够执行phantomjs脚本,由$output判断;它只是因为某种原因坠毁。

我从你的日志谷歌的第一个错误(QXcbConnection:无法连接到显示)和这里的官方PhantomJS github上与工作解决方案这个问题的discussino,希望它有助于:https://github.com/ariya/phantomjs/issues/14376

+0

$输出使我困惑了很长时间,但我认为问题是使用php或ubuntu。现在,我确认问题是与phantomjs或我的js文件,谢谢! –

+0

4,你使用二进制PhantomJS还是你从源代码编译它? 二进制 - 将它安装为Ubuntu的一部分(使用apt).III二进制Phantomsj是什么?它与'sudo apt-get install phantomjs'有什么区别? –

+0

请更改您的最新评论,并不确定您的意思。但无论如何,你最好从官方回购获得二进制。 – Vaviloff