简体   繁体   English

通过PHP从命令行运行程序-传递命令,而不是每次都重新启动程序

[英]Running program from the command line via PHP - Passing commands instead of restarting the program every time

I'm using a local server on my laptop to control a C# program via PHP. 我在笔记本电脑上使用本地服务器通过PHP控制C#程序。 Basically I'm taking a POST passed to my web server, and using it as the parameters for a command line program. 基本上,我将POST传递到我的Web服务器,并将其用作命令行程序的参数。 My code is essentially this: 我的代码本质上是这样的:

$parameters = $_POST['parameters'];
system('C://THEFILEPATH/myprogram.exe ' . $parameters);

The problem is that this causes myprogram.exe to stop and start every time I want to pass something to it. 问题是,这会使myprogram.exe每次我想传递一些东西时都停止并启动。 Since a good portion of my program is initialization, it causes a bit of unnecessary lag. 由于程序的很大一部分是初始化,因此会引起一些不必要的延迟。 Is there a way to run myprogram.exe via PHP, and pass it variables as it continues to run instead of starting it anew each time? 有没有一种方法可以通过PHP运行myprogram.exe,并在继续运行时传递变量,而不是每次都重新启动它?

Thanks! 谢谢!

take the input and write it to a file. 接受输入并将其写入文件。 your myprogram should read the file every once in a while and delete all read input so that the file works like a queue. 您的myprogram应该不时读取文件,并删除所有读取的输入,以便文件像队列一样工作。 if you want to always run your program and while running receive your input then you should consider implementing a component for listening to a special network port. 如果您想始终运行程序,并且在运行时收到您的输入,则应考虑实现侦听特殊网络端口的组件。

http://www.php.net/manual/en/function.proc-open.php This provides bidirectional support (reading and writing). http://www.php.net/manual/zh/function.proc-open.php这提供了双向支持(读取和写入)。 I'm not sure how tested this is on Windows, though. 不过,我不确定在Windows上如何进行测试。 It says later on there: 它稍后说:

Windows compatibility: Descriptors beyond 2 (stderr) are made available to the child process as inheritable handles, but since the Windows architecture does not associate file descriptor numbers with low-level handles, the child process does not (yet) have a means of accessing those handles. Windows兼容性:子进程可以使用2以上的描述符(stderr)作为可继承句柄,但是由于Windows体系结构并未将文件描述符编号与低级句柄相关联,因此子进程(尚未)没有访问方法这些手柄。 Stdin, stdout and stderr work as expected. Stdin,stdout和stderr可以正常工作。

So you should be able to write to it just like a file. 因此,您应该能够像文件一样对其进行写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM