dqzlqfqeh845799833 2018-12-19 21:21 采纳率: 0%
浏览 47
已采纳

从php Web应用程序执行节点脚本(Windows)

I have a php web application and on one of the route I try to execute node script.

Node script location: C:\Users\meusr\Workspace\www\myproject\directImport.js

I have configured my package.json, such that npm start executes above script.

php (laravel) application's executable index.php located at: C:\Users\meusr\Workspace\www\myPHPproject\public\

And on one of my route: importer/run

I have:

$commandToRun = "npm start --prefix ". env('IMPORTER_PATH'). " /dev/null 2>&1";

Where IMPORTER_PATH is configured in my env, as: IMPORTER_PATH = C:\Users\meusr\Workspace\www\myproject\

when I try to execute the command with exec

exec($commandToRun, $output);

Page keeps on loading no output, no error and no end.

The script has no issue and running the same command on command line on windows system works:

npm start --prefix C:\Users\meusr\Workspace\www\myproject\

outputs as expected.

I thought it was issue with permission first (which should have thrown error, but I still tried and ran another command)

exec('npm -v', $output);

which outputs my npm version.

Similarly I tried to use simply node directImport.js which didn't work either.

Then tried to change directory to the location where node file is located and ran the command again. exec(cd C:\Users\meusr\Workspace\www\myproject\ && dir /dev/null 2>&1) // this works, but:

exec(cd C:\Users\meusr\Workspace\www\myproject\ && node directImport.js /dev/null 2>&1) // this didn't work

  • 写回答

1条回答 默认 最新

  • dongxiejie9387 2018-12-28 12:17
    关注

    Thanks to miken32, what worked for me was:

    <?php 
    $commandString = 'start /b c:\\programToRun.exe -attachment "c:\\temp\file1.txt"'; 
    pclose(popen($commandString, 'r')); 
    ?>
    

    http://php.net/manual/en/function.popen.php#92413

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀