php - 从 PHP 发送一个值到 Node JS 执行

标签 php node.js apache shell-exec

大家好!

我有一个名为 start.php 的文件,在该文件中我将 x 的值设置为 5。我还有另一个名为 check.js 的文件

在我的 PHP 文件中,我使用 shell_exec 运行 check.js

我的问题是,我应该怎么做才能让 check.js 检查 start.php 中 x 的值

使用 shell_exec 时可以这样做吗?如果不是我该怎么办?

致以诚挚的问候

最佳答案

调用 check.js 时,您可以在参数中传递 x

假设您有 check.js 位于这样的文件夹中 c:\apps\check.js 这里是一些您可以尝试的代码:

start.php

<?php

$x = 5;

$output = shell_exec("node.exe c:\apps\check.js x=$x");

echo "<pre>$output</pre>";

?>
<小时/>

c:\apps\check.js

const querystring = require('querystring');

const data = querystring.parse( process.argv[2] || '' );

const x = data.x;

console.log(x);
<小时/>

Node.js 代码使用 querystring 模块 ( https://nodejs.org/api/querystring.html ) 来解析 x

Update (if you need to pass more than one value)

start.php

<?php

$x = 5;
$y = 7;

$output = shell_exec("node.exe c:\apps\check.js x=$x+y=$y");

echo "<pre>$output</pre>";

?>
<小时/>

c:\apps\check.js

const querystring = require('querystring');

const data = querystring.parse( process.argv[2] || '', '+' );

console.log(data.x);
console.log(data.y);

<小时/> 我希望这会有所帮助。

关于php - 从 PHP 发送一个值到 Node JS 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51775525/

相关文章:

php - 如何使用Elasticsearch将过滤器与距离结合起来以进行搜索?

php - 集合中所有可能的组合

node.js - GruntJS + contrib-咖啡 : compiling but keeping the folder structure

tomcat - Apache HTTP Server 和 Tomcat 在一台机器上

java - 我可以将 Apache Shiro 用于分层用户模型吗?

php - 如何显示所有id_following

php - 为什么我收到 Swift_TransportException : Unable to connect with TLS encryption in Server?

google-app-engine - 使用 Mocha 对 OAuth JS 进行单元测试

node.js - Nodejs express 请求 header - 获取引荐来源网址等。

security - .htaccess 密码保护有多安全?