从PHP向Node.js传递HTML

从PHP向Node.js传递HTML,php,node.js,command-line-interface,Php,Node.js,Command Line Interface,我有一个主要用PHP编写的应用程序,但是有一个npm包,它的功能需要整合到我的应用程序中。我必须将一个HTML字符串传递到Node.js应用程序中,但在纠正问题时遇到了问题。我正在使用: exec('node '.$rootPath.'node/app.js '.$imageId.' email '.escapeshellcmd($emailString).' 2>&1', $output, $retVar); 将数据发送到我的Node.js应用程序,但我不确定一旦数据到达那里如

我有一个主要用
PHP
编写的应用程序,但是有一个
npm
包,它的功能需要整合到我的应用程序中。我必须将一个
HTML
字符串传递到
Node.js
应用程序中,但在纠正问题时遇到了问题。我正在使用:

exec('node '.$rootPath.'node/app.js '.$imageId.' email '.escapeshellcmd($emailString).' 2>&1', $output, $retVar);
将数据发送到我的
Node.js
应用程序,但我不确定一旦数据到达那里如何解码,需要通过
JavaScript
进行处理。有没有办法在JavaScript中取消scape
escapeshellcmd()
?或者有没有其他方法可以通过命令行传递这些长字符串
HTML

编辑:下面是我用来将信息传递给Node.js的确切方法:

    try{
        $emailString = escapeshellcmd($decoded);
        //`node $rootPath'node/app.js' $imageId email $emailString 2>&1`;
        exec('node '.$rootPath.'node/app.js '.$imageId.' email "'.$emailString.'" 2>&1', $output, $retVar);
        print_r($output);
    }catch(Exception $e){
        echo $e->getMessage()."\n";
    }
下面是app.js:

process.argv.forEach(function(value, index, array){
     if(index == 2){
        id = value;
     }
     if(index == 3){
        type = value;
    }
    if(index == 4){
        visual = value;
    }
});
console.log('******* FROM NODE********');
console.log(visual);
看起来只有第一行被传递或收集并打印回来,而且看起来它仍然被编码(除非控制台在打印时重新编码)。此外,我也不确定为什么它似乎是附加值而不是覆盖值:

Array
(
    [0] => ******* FROM NODE********
\<head\>\<style type=text/css\>body \{padding:0\; margin:0\; text-align:center\;.tbl1 \{background-color:\#a53f0f\; color:\#fff\; text-align:center\; font-size:\<body data-gramm=true data-gramm_editor=true data-gramm_id=ccdbd45c-b0bf-4691-9\<table border=0 cellpadding=0 cellspacing=0 style=background-color:
)

Array
(
    [0] => ******* FROM NODE********
\<head\>\<style type=text/css\>body \{padding:0\; margin:0\; text-align:center\;.tbl1 \{background-color:\#a53f0f\; color:\#fff\; text-align:center\; font-size:\<body data-gramm=true data-gramm_editor=true data-gramm_id=ccdbd45c-b0bf-4691-9\<table border=0 cellpadding=0 cellspacing=0 style=background-color:
    [2] => ******* FROM NODE********
\<html xmlns=http://www.w3.org/1999/xhtml xmlns:v=urn:schemas-microsoft-com:vml         \<meta name=viewport content=width=device-width,e\>
)
数组
(
[0]=>******来自节点********

\\正文\{填充:0 \;边距:0 \;文本对齐:中心\。tbl1 \{背景色:a53f0f \;颜色:fff \;文本对齐:中心\;字体大小:\注意:对于要处理的数据,应该在参数上使用流。这是Unix世界中命令工作的常见方式

在代码中,您尝试使用
escapeshellcmd
来转义双引号
一个封装的参数字符串。这不起作用。还有一个
escapeshellarg
PHP函数。它将字符串封装在单引号
中,并转义字符,这些字符甚至包含在shell以特殊方式处理的单引号字符串中

假设
$decoded

$decoded = '<body lang="en">very boring message</body>';

上面提到的,您确实应该考虑使用流而不是参数。优点是数据可以增长到任意大小。此外,代码<> PROXOPENG/<代码>句柄<代码> StdOUT 和<代码> STDRR 单独。

try
{
  if($handle = proc_open("node app.js {$imageId} email";, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $streams))
  {
    [$stdin, $stdout, $stderr] = $streams;

    fwrite($stdin, $decoded);
    fclose($stdin);

    $output = stream_get_contents($stdout);
    fclose($stdout);

    $error  = stream_get_contents($stderr);
    fclose($stderr);

    proc_close($handle);
  }

  echo 'OUTPUT:', PHP_EOL, $output, PHP_EOL;
  echo 'ERRORS:', PHP_EOL, $error, PHP_EOL;

}
catch(Exception $e)
{
  echo $e->getMessage(), PHP_EOL;
}
下面是一个示例node.js脚本,它通过arg和stdin处理数据:

(() =>
{
  'use strict';

  console.log('******* FROM NODE********');

  const
    getStdin = require('get-stdin');

  var id, type, visual;
  [,, id, type, visual] = process.argv;


  // if 4th command line argument is present, use that
  if(undefined !== visual)
    processData(visual);

  // otherwise read data from stdin stream
  else
    getStdin().then(visual =>
    {
      processData(visual);
    });


  function processData(data)
  {
    console.log('id'  , id  );
    console.log('type', type);


    console.log('STDIN:', data);
    console.error('no errors');

    console.log('******* DONE *******');
  }

})();

虽然卡西莫多的克隆答案确实有效,但他对这个问题的评论让我想到了传递大字符串的HTML。相反,我选择将HTML写入文件并使用id引用。

为什么要使用
2>&1
。无需丢弃输出。@srimaln91它没有被丢弃,但
stderr
被重定向到e> stdout
-与在终端中查看的结果相同。丢弃将是
2>/dev/null
@srimaln91但是,我更喜欢使用
proc\u open
@Quasimodo's单独处理所有标准流。请查看编辑。首先+1向上投票。另一个独立于问题的问题:您真的需要通过邮件吗处理文本数据的常用方法是充当标准输入和标准输出流之间的过滤器。
(() =>
{
  'use strict';

  console.log('******* FROM NODE********');

  const
    getStdin = require('get-stdin');

  var id, type, visual;
  [,, id, type, visual] = process.argv;


  // if 4th command line argument is present, use that
  if(undefined !== visual)
    processData(visual);

  // otherwise read data from stdin stream
  else
    getStdin().then(visual =>
    {
      processData(visual);
    });


  function processData(data)
  {
    console.log('id'  , id  );
    console.log('type', type);


    console.log('STDIN:', data);
    console.error('no errors');

    console.log('******* DONE *******');
  }

})();