亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試從 PHP 頁面執行 node.js 命令

嘗試從 PHP 頁面執行 node.js 命令

PHP
料青山看我應如是 2021-11-13 10:28:22
我正在嘗試從 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 中的建議,它們不會產生任何語法錯誤但會靜默失?。╪ode.js 程序未運行且未創建 HTML 表)。我還確認運行 PHP 腳本的用戶和 node.js 命令是相同的(或者至少嘗試使用 PHP 的 get_current_user();理想情況下,我只想從 PHP 頁面運行 node.js 應用程序,而不必等待輸出并將 HTML 時間表插入到目錄中以備將來使用。PHP 頁面甚至不使用時間表。直接從命令行運行 node.js 應用程序時,這一切都是可能的,但不能從 PHP 頁面運行。
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您的方法#1 中的錯誤是舊版本節點(7.6 之前)無法識別“async”關鍵字的癥狀。嘗試添加:

console.log(process.version)

到節點命令仔細檢查這一點。如果它實際上是舊版本的節點,請更新它。

使用方法#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


查看完整回答
反對 回復 2021-11-13
?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

require 可能在路徑中找不到包,因為它實際上是從 PHP 文件位置執行的,我認為您還需要將路徑添加到與 PHP 執行位置相關的節點模塊中。

但由于它說語法錯誤,我需要查看完整文件。


查看完整回答
反對 回復 2021-11-13
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

在這種情況下,您需要安裝node php


編碼:


<?php



error_reporting(E_ALL);


set_time_limit(120);


define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished


define("NODE_VER", "v9.1.0");


define("NODE_ARCH", "x" . substr(php_uname("m"), -2)); //x86 or x64


define("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() {

    if(file_exists(NODE_DIR)) {

        echo "Node.js is already installed.\n";

        return;

    }

    if(!file_exists(NODE_FILE)) {

        echo "Downloading Node.js from " . NODE_URL . ":\n\n";

        $fp = fopen(NODE_FILE, "w");

        flock($fp, LOCK_EX);

        $curl = curl_init(NODE_URL);

        curl_setopt($curl, CURLOPT_HEADER, 0);

        curl_setopt($curl, CURLOPT_FILE, $fp);

        $resp = curl_exec($curl);

        curl_close($curl);

        flock($fp, LOCK_UN);

        fclose($fp);

        echo $resp === true ? "Done.\n" : "Failed. Error: curl_error($curl)\n";

    }

    echo "Installing Node.js:\n";

    passthru("tar -xzf " . NODE_FILE . " 2>&1 && mv node-" . NODE_VER . "-linux-" . NODE_ARCH . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret);

    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() {

    if(!file_exists(NODE_DIR)) {

        echo "Node.js is not yet installed.\n";

        return;

    }

    echo "Unnstalling Node.js:\n";

    passthru("rm -rfv " . NODE_DIR . " nodepid", $ret);

    passthru("rm -rfv node_modules", $ret);

    passthru("rm -rfv .npm", $ret);

    passthru("rm -rfv nodeout", $ret);

    echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";

}


function node_start($file) {

    if(!file_exists(NODE_DIR)) {

        echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";

        return;

    }

    $node_pid = intval(file_get_contents("nodepid"));

    if($node_pid > 0) {

        echo "Node.js is already running. <a href='?stop'>Stop it</a>.\n";

        return;

    }

    $file = escapeshellarg($file);

    echo "Starting: node $file\n";

    $node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!");

    echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n";

    file_put_contents("nodepid", $node_pid, LOCK_EX);

    sleep(1); //Wait for node to spin up

    echo file_get_contents("nodeout");

}


function node_stop() {

    if(!file_exists(NODE_DIR)) {

        echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";

        return;

    }

    $node_pid = intval(file_get_contents("nodepid"));

    if($node_pid === 0) {

        echo "Node.js is not yet running.\n";

        return;

    }

    echo "Stopping Node.js with PID=$node_pid:\n";

    $ret = -1;

    passthru("kill $node_pid", $ret);

    echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";

    file_put_contents("nodepid", '', LOCK_EX);

}


function node_npm($cmd) {

    if(!file_exists(NODE_DIR)) {

        echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";

        return;

    }

    $cmd = escapeshellcmd(NODE_DIR . "/bin/npm --cache ./.npm -- $cmd");

    echo "Running: $cmd\n";

    $ret = -1;

    passthru($cmd, $ret);

    echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret. See <a href=\"npm-debug.log\">npm-debug.log</a>\n";

}


function node_serve($path = "") {

    if(!file_exists(NODE_DIR)) {

        node_head();

        echo "Node.js is not yet installed. Switch to Admin Mode and <a href='?install'>Install it</a>.\n";

        node_foot();

        return;

    }

    $node_pid = intval(file_get_contents("nodepid"));

    if($node_pid === 0) {

        node_head();

        echo "Node.js is not yet running. Switch to Admin Mode and <a href='?start'>Start it</a>\n";

        node_foot();

        return;

    }

    $curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");

    curl_setopt($curl, CURLOPT_HEADER, 1);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $headers = array();

        foreach(getallheaders() as $key => $value) {

                $headers[] = $key . ": " . $value;

        }

        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);

        if($_SERVER["REQUEST_METHOD"] === "POST") {

                curl_setopt($curl, CURLOPT_POST, 1);

                curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));

        }

    $resp = curl_exec($curl);

    if($resp === false) {

        node_head();

        echo "Error requesting $path: " . curl_error($curl);

        node_foot();

    } else {

        list($head, $body) = explode("\r\n\r\n", $resp, 2);

        $headarr = explode("\n", $head);

        foreach($headarr as $headval) {

            header($headval);

        }

        echo $body;

    }

    curl_close($curl);

}


function node_head() {

    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() {

    echo '</pre><p><a href="https://github.com/niutech/node.php" target="_blank">Powered by node.php</a></p></body></html>';

}


function node_dispatch() {

    if(ADMIN_MODE) {

        node_head();

        if(isset($_GET['install'])) {

            node_install();

        } elseif(isset($_GET['uninstall'])) {

            node_uninstall();

        } elseif(isset($_GET['start'])) {

            node_start($_GET['start']);

        } elseif(isset($_GET['stop'])) {

            node_stop();

        } elseif(isset($_GET['npm'])) {

            node_npm($_GET['npm']);

        } else {

            echo "You are in Admin Mode. Switch back to normal mode to serve your node app.";

        }

        node_foot();

    } else {

        if(isset($_GET['path'])) {

            node_serve($_GET['path']);

        } else {

            node_serve();

        }

    }

}


node_dispatch();


查看完整回答
反對 回復 2021-11-13
  • 3 回答
  • 0 關注
  • 275 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號