简体   繁体   English

使用exec()从PHP运行PhantomJS

[英]Running PhantomJS from PHP with exec()

I've got the following script: 我有以下脚本:

#!/bin/sh
export DISPLAY=:0
phantomjs --version

It try to run it from the following PHP script: 它尝试从以下PHP脚本运行它:

<?php
$result = shell_exec('sh test.sh'); 
echo $result;
?>

This script return the following error: 该脚本返回以下错误:

[Thu Jun 19 10:31:31 2014] [error] [client] test.sh: line 3: phantomjs: command not found

I tried to run phantomjs -v by hand in a console, and it runs fine. 我试图在控制台中手动运行phantomjs -v ,它运行正常。 I checked the PATH , and phantomjs is correctly defined and found. 我检查了PATH ,并正确定义并找到了phantomjs。

The execution environment is a virtual Server with LiveConfig. 执行环境是带有LiveConfig的虚拟服务器。 Can someone help me understand what I'm doing wrong ? 有人可以帮助我了解我在做什么错吗?

It could be an issue with shell_exec() and line breaks, try adding "2>&1" to the string you are passing: shell_exec()和换行符可能是一个问题,请尝试在传递的字符串中添加“ 2>&1”:

$result = shell_exec('sh test.sh 2>&1'); 

this worked for me, found it in the top comment here, naturally ;) 这对我有用,很自然地在这里的热门评论中找到了它;)

Your PATH probably lacks the location for the phantomjs executable. 您的PATH可能缺少phantomjs可执行文件的位置。 PhantomJS is probably installed in /usr/local/bin so you need to add this to your PATH variable: PhantomJS可能安装在/usr/local/bin因此需要将其添加到PATH变量中:

#!/bin/sh
export DISPLAY=:0
PATH=$PATH:/usr/local/bin
phantomjs --version

To check what the current PATH is, you could begin the shell script with: 要检查当前的PATH是什么,可以使用以下命令开始shell脚本:

#!/bin/sh
echo $PATH
<?php
         exec('/usr/local/bin/phantomjs path/somescript.js');
?>

Yes. 是。 Sometimes phantomjs don't need full path in some environment without generate any error. 有时,phantomjs在某些环境中不需要完整路径而不会产生任何错误。 However, sometimes it does. 但是,有时确实如此。

Always use the full path for all argument in the php command. 始终对php命令中的所有参数使用完整路径。

Did you use the fullpath for hello.js? 您是否为hello.js使用了全路径?

Do not use exec(). 不要使用exec()。 Never. 决不。 It's a bad way. 这是一个坏方法。

Use the php-phantomjs and PhantomJS Runner instead. 请改用php-phantomjsPhantomJS Runner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM