尝试从 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.仅从 PHP 页面运行 node 命令,此 node.js 应用程序将接受该命令以及“config”文件参数,该参数指定一个配置文件,该文件下载要转换为 HTML 的数据(异步)


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


3.我也尝试过这些先前问题Here和Here 中的建议,它们不会产生任何语法错误但会静默失败(node.js 程序未运行且未创建 HTML 表)。


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


 get_current_user();

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


料青山看我应如是
浏览 204回答 3
3回答

慕姐8265434

您的方法#1 中的错误是旧版本节点(7.6 之前)无法识别“async”关键字的症状。尝试添加:console.log(process.version)到节点命令仔细检查这一点。如果它实际上是旧版本的节点,请更新它。使用方法#2,您尝试将 app.js 作为 shell 脚本运行。该错误表明 bash 正在尝试运行该文件,并且显然无法识别 javascript 语法。你应该使用:$command&nbsp;=&nbsp;'export&nbsp;PATH=$PATH:path-to-node-executable;&nbsp;node&nbsp;app.js';一旦您使用这两种方法中的任何一种,您确实应该能够通过遵循此处的答案来实现理想的情况:php exec command (or similar) to not wait for result

人到中年有点甜

require 可能在路径中找不到包,因为它实际上是从 PHP 文件位置执行的,我认为您还需要将路径添加到与 PHP 执行位置相关的节点模块中。但由于它说语法错误,我需要查看完整文件。

噜噜哒

在这种情况下,您需要安装node php编码:<?phperror_reporting(E_ALL);set_time_limit(120);define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finisheddefine("NODE_VER", "v9.1.0");define("NODE_ARCH", "x" . substr(php_uname("m"), -2)); //x86 or x64define("NODE_FILE", "node-" . NODE_VER . "-linux-" . NODE_ARCH . ".tar.gz");define("NODE_URL", "http://nodejs.org/dist/" . NODE_VER . "/" . NODE_FILE);define("NODE_DIR", "node");define("NODE_PORT", 49999);function node_install() {&nbsp; &nbsp; if(file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is already installed.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; if(!file_exists(NODE_FILE)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Downloading Node.js from " . NODE_URL . ":\n\n";&nbsp; &nbsp; &nbsp; &nbsp; $fp = fopen(NODE_FILE, "w");&nbsp; &nbsp; &nbsp; &nbsp; flock($fp, LOCK_EX);&nbsp; &nbsp; &nbsp; &nbsp; $curl = curl_init(NODE_URL);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_HEADER, 0);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_FILE, $fp);&nbsp; &nbsp; &nbsp; &nbsp; $resp = curl_exec($curl);&nbsp; &nbsp; &nbsp; &nbsp; curl_close($curl);&nbsp; &nbsp; &nbsp; &nbsp; flock($fp, LOCK_UN);&nbsp; &nbsp; &nbsp; &nbsp; fclose($fp);&nbsp; &nbsp; &nbsp; &nbsp; echo $resp === true ? "Done.\n" : "Failed. Error: curl_error($curl)\n";&nbsp; &nbsp; }&nbsp; &nbsp; echo "Installing Node.js:\n";&nbsp; &nbsp; passthru("tar -xzf " . NODE_FILE . " 2>&1 && mv node-" . NODE_VER . "-linux-" . NODE_ARCH . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret);&nbsp; &nbsp; echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\nTry putting node folder via (S)FTP, so that " . __DIR__ . "/node/bin/node exists.";}function node_uninstall() {&nbsp; &nbsp; if(!file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet installed.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; echo "Unnstalling Node.js:\n";&nbsp; &nbsp; passthru("rm -rfv " . NODE_DIR . " nodepid", $ret);&nbsp; &nbsp; passthru("rm -rfv node_modules", $ret);&nbsp; &nbsp; passthru("rm -rfv .npm", $ret);&nbsp; &nbsp; passthru("rm -rfv nodeout", $ret);&nbsp; &nbsp; echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";}function node_start($file) {&nbsp; &nbsp; if(!file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $node_pid = intval(file_get_contents("nodepid"));&nbsp; &nbsp; if($node_pid > 0) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is already running. <a href='?stop'>Stop it</a>.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $file = escapeshellarg($file);&nbsp; &nbsp; echo "Starting: node $file\n";&nbsp; &nbsp; $node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!");&nbsp; &nbsp; echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n";&nbsp; &nbsp; file_put_contents("nodepid", $node_pid, LOCK_EX);&nbsp; &nbsp; sleep(1); //Wait for node to spin up&nbsp; &nbsp; echo file_get_contents("nodeout");}function node_stop() {&nbsp; &nbsp; if(!file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $node_pid = intval(file_get_contents("nodepid"));&nbsp; &nbsp; if($node_pid === 0) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet running.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; echo "Stopping Node.js with PID=$node_pid:\n";&nbsp; &nbsp; $ret = -1;&nbsp; &nbsp; passthru("kill $node_pid", $ret);&nbsp; &nbsp; echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";&nbsp; &nbsp; file_put_contents("nodepid", '', LOCK_EX);}function node_npm($cmd) {&nbsp; &nbsp; if(!file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $cmd = escapeshellcmd(NODE_DIR . "/bin/npm --cache ./.npm -- $cmd");&nbsp; &nbsp; echo "Running: $cmd\n";&nbsp; &nbsp; $ret = -1;&nbsp; &nbsp; passthru($cmd, $ret);&nbsp; &nbsp; echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret. See <a href=\"npm-debug.log\">npm-debug.log</a>\n";}function node_serve($path = "") {&nbsp; &nbsp; if(!file_exists(NODE_DIR)) {&nbsp; &nbsp; &nbsp; &nbsp; node_head();&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet installed. Switch to Admin Mode and <a href='?install'>Install it</a>.\n";&nbsp; &nbsp; &nbsp; &nbsp; node_foot();&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $node_pid = intval(file_get_contents("nodepid"));&nbsp; &nbsp; if($node_pid === 0) {&nbsp; &nbsp; &nbsp; &nbsp; node_head();&nbsp; &nbsp; &nbsp; &nbsp; echo "Node.js is not yet running. Switch to Admin Mode and <a href='?start'>Start it</a>\n";&nbsp; &nbsp; &nbsp; &nbsp; node_foot();&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; $curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");&nbsp; &nbsp; curl_setopt($curl, CURLOPT_HEADER, 1);&nbsp; &nbsp; curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);&nbsp; &nbsp; &nbsp; &nbsp; $headers = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach(getallheaders() as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $headers[] = $key . ": " . $value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);&nbsp; &nbsp; &nbsp; &nbsp; if($_SERVER["REQUEST_METHOD"] === "POST") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_POST, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; $resp = curl_exec($curl);&nbsp; &nbsp; if($resp === false) {&nbsp; &nbsp; &nbsp; &nbsp; node_head();&nbsp; &nbsp; &nbsp; &nbsp; echo "Error requesting $path: " . curl_error($curl);&nbsp; &nbsp; &nbsp; &nbsp; node_foot();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; list($head, $body) = explode("\r\n\r\n", $resp, 2);&nbsp; &nbsp; &nbsp; &nbsp; $headarr = explode("\n", $head);&nbsp; &nbsp; &nbsp; &nbsp; foreach($headarr as $headval) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header($headval);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $body;&nbsp; &nbsp; }&nbsp; &nbsp; curl_close($curl);}function node_head() {&nbsp; &nbsp; echo '<!DOCTYPE html><html><head><title>Node.php</title><meta charset="utf-8"><body style="font-family:Helvetica,sans-serif;"><h1>Node.php</h1><pre>';}function node_foot() {&nbsp; &nbsp; echo '</pre><p><a href="https://github.com/niutech/node.php" target="_blank">Powered by node.php</a></p></body></html>';}function node_dispatch() {&nbsp; &nbsp; if(ADMIN_MODE) {&nbsp; &nbsp; &nbsp; &nbsp; node_head();&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_GET['install'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_install();&nbsp; &nbsp; &nbsp; &nbsp; } elseif(isset($_GET['uninstall'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_uninstall();&nbsp; &nbsp; &nbsp; &nbsp; } elseif(isset($_GET['start'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_start($_GET['start']);&nbsp; &nbsp; &nbsp; &nbsp; } elseif(isset($_GET['stop'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_stop();&nbsp; &nbsp; &nbsp; &nbsp; } elseif(isset($_GET['npm'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_npm($_GET['npm']);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "You are in Admin Mode. Switch back to normal mode to serve your node app.";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; node_foot();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if(isset($_GET['path'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_serve($_GET['path']);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node_serve();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}node_dispatch();
打开App,查看更多内容
随时随地看视频慕课网APP