php - 尝试从 PHP 页面执行 node.js 命令

标签 php node.js

我正在尝试从 PHP 页面执行 node.js 命令,该命令在通过 SSH 从 Linux 终端运行时成功执行,但我无法让它在没有错误的情况下从 PHP 页面运行。

环境是 Apache CentOS VPS 托管帐户。

我对 PHP 比 Node.js 更熟悉。 Node.js 应用程序将从 URL 下载的一些包含时间表的压缩文本文件(包含时间表信息)转换为 HTML,然后将 HTML 文件复制到服务器根目录中的指定目录中。下载时间表的 URL 位置位于异步加载的 config.json 文件中。当尝试从 PHP 页面运行它时,我使用此函数在测试时显示输出:

function liveExecuteCommand($cmd)       {           
    while (@ ob_end_flush()); // end all output buffers if any
        $proc = popen("$cmd 2>&1 ; echo Exit status : $?", 'r');
        $live_output     = "";          $complete_output = "";
        while (!feof($proc))            {
                $live_output     = fread($proc, 4096);
                $complete_output = $complete_output . $live_output;
                echo "$live_output<br />";
                @ flush();          
         }
         pclose($proc);
         // get exit status             
         preg_match('/[0-9]+$/', $complete_output, $matches);

         // return exit status and intended output          
         return array (
                'exit_status'  => intval($matches[0]),
                'output'       => str_replace("Exit status : " . $matches[0], '', $complete_output)
                         );         
}

然后,要在 PHP 页面上查看结果,我使用以下命令:

   $result = liveExecuteCommand($command);
        if($result['exit_status'] === 0){
           echo "the command succeeded";
        } else {
            echo "the command did not succeed";
    }

这个函数可以工作并允许我查看输出,当从页面运行时,输出总是包含 JavaScript 语法错误的通知,但不包含从命令行运行时的通知。我似乎无法让 Node.js 应用程序在 PHP 页面没有语法错误的情况下运行。我尝试了几件事:

1. 仅从该 Node.js 应用程序接受的 PHP 页面运行一个 Node 命令,以及一个“配置”文件参数,该参数指定一个配置文件,用于下载要转换为 HTML 的数据(异步)

  $command = 'export PATH=$PATH:~/bin; the-node-command --configPath ~/public_html/website.com/gtfs-to-html-configs/config.json'; 

这是我收到的错误:

 //get configuration file necessary for node.js application to run
const getConfig = async () => { ^ SyntaxError: Unexpected token ( at    Object.exports.runInThisContext (vm.js:53:16) 

2.通过 NPM 组合一个 Node 项目并安装所有依赖项等,然后在该包中运行一个 node.js 文件来完成同样的事情:

 $command = 'export PATH=$PATH:~/transit-app; app.js'; 

这是我收到的错误:

   //it's happening at the 'require' line at beginning of the app.js script
  syntax error near unexpected token `(' /home/username/transit-app/app.js:line 1: `const gtfsToHTML = require('gtfs-to-html');' 

3.我还尝试了之前这些问题中的建议 HereHere它不会生成任何语法错误,但会默默地失败(node.js 程序不会运行,也不会创建 HTML 表)。

我还确认运行 PHP 脚本和 node.js 命令的用户是相同的(或者至少尝试使用 PHP 来验证它

 get_current_user();

理想情况下,我只想从 PHP 页面运行 node.js 应用程序,而不必等待输出,并将 HTML 时间表插入到目录中以供将来使用。 PHP 页面甚至不使用时间表。直接从命令行运行 node.js 应用程序时,这一切都是可能的,但不能从 PHP 页面运行。

更新#1

包含从命令行运行的 gtfs-to-html node.js 应用程序的文件 (gtfs-to-html): https://pastebin.com/dHecqmf8

gtfs-to-html 制作 HTML 时间表所需的 json 配置文件: https://pastebin.com/4a4MKuPd

我尝试从中运行node.js命令的php页面: https://pastebin.com/5JczB76T

找到了 gtfs-to-html 命令并且运行了应用程序,因此我知道它位于我指定的目录中,但在尝试从 php 页面运行该命令时,仍然在 node.js 应用程序文件中遇到此语法错误:

 Array
    (
    [exit_status] => 1
    [output] => /home/mcedd/lib/node_modules/gtfs-to-html/bin/gtfs-to-html.js:39
const getConfig = async () => {
                        ^
SyntaxError: Unexpected token (
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (node.js:348:7)
    at startup (node.js:140:9)
    at node.js:463:3


    )

最佳答案

方法 #1 中的错误是旧版本 Node (7.6 之前的版本)无法识别“async”关键字的症状。尝试添加:

console.log(process.version)

到-node-命令来仔细检查这一点。如果它实际上是旧版本的 Node ,请更新它。

使用方法 #2,您尝试将 app.js 作为 shell 脚本运行。该错误表明 bash 正在尝试运行该文件,并且显然无法识别 javascript 语法。您应该使用:

$command = 'export PATH=$PATH:path-to-node-executable; node app.js';

一旦您使用了这两种方法中的任何一种,您确实应该能够通过遵循此处的答案来实现您的理想情况:php exec command (or similar) to not wait for result

关于php - 尝试从 PHP 页面执行 node.js 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57342576/

相关文章:

javascript - 无法找到文件。 NodeJS、MySQL、Visual Studio 代码

javascript - 在事件函数中检查组件状态是否有效?

mysql - PassportJS 本地策略中的这个选项对象参数有什么用?

php - jQuery 将表单数据发送到 PHP

php - 将mysql查询限制为1个结果?

PHP 使用偏移量和长度来替换多个字符串

node.js - CakePHP 精益 Controller 方法每分钟被调用多次

PHP 在目录中找不到我的 header.html 文件

php - 2 个不同的小查询与 1 个带有子查询的查询

node.js - Bootstrap 突然不适用于我的 React JS 项目