Sending commands to windows command line (cmd) from PHP

喜夏-厌秋 提交于 2019-12-11 18:08:28

问题


Is this possible?

What I want to do is send:

run_app.exe -param 'test' -name 'tester'

to a windows cmd line from PHP.

Is this possible or do I need to write a windows service that is somehow triggered by the application?


回答1:


You can use exec() for that.




回答2:


Have you tried exec?




回答3:


I suggest using standard PHP system command. There is plenty of info and examples in official PHP site.

There is an answered question on SO for differences between exec(), system() and passthru().




回答4:


Or you can use:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run(strCommand, [intWindowStyle], [bWaitOnReturn]);

Here you can find the run method params: http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.85%29.aspx

And here is the COM class doc: http://www.php.net/manual/en/class.com.php

With this method you can do so much more in windows :). I used it becaus of the [bWaitOnReturn] parameter which I couldn't do using any other method.




回答5:


Here is a project that allows PHP to obtain and interact dynamically with a real cmd terminal. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//if you prefer Powershell, replace 'cmd' with 'powershell'
$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('cmd');

$strCmd1   = 'run_app.exe -param "test" -name "tester"';
$return1   = $shellObj->exeCmd($strCmd1);

The return will give you the command return OR error from cmd, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.



来源:https://stackoverflow.com/questions/4450169/sending-commands-to-windows-command-line-cmd-from-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!