dongpu3792 2018-08-09 20:47
浏览 212
已采纳

从PHP发送值到Node JS执行

Hello everyone!

I have a file called start.php and in this file I have set the value of x to 5. I have another file called check.js

In my PHP file I use shell_exec to run check.js

My question is, what should I do to make check.js check the value of x which is in start.php

Is it possible to do it this way while using shell_exec? If not what should I do?

Best regards

  • 写回答

1条回答 默认 最新

  • douzou7012 2018-08-10 05:47
    关注

    You could pass x in arguments when calling check.js

    Assuming you have check.js located in a folder like this c:\apps\check.js here is some code you can try:

    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 code is using querystring module (https://nodejs.org/api/querystring.html) for parsing 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);
    


    I hope this helps.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建