Php server response to android when executing a ba

2019-08-09 06:56发布

问题:

I have an application which is in java & i have created a batch file(.bat) of that application & running it in php like this

   <?php
      system ("cmd.exe /c final.bat");
   ?>

The output is something like this

C:\wamp\www\php>java -jar Most closely reseambling is this...C:\wamp\www\php>pause Press any key to continue . . .

I am invoking this through my android application & want this out as response...Can you tell me how to do this .... It would be great if I would be able to eliminate prompts from response.

回答1:

Check out the exec function. Instead of automatically printing the output, it will return the output as a string, or in an optional array of lines in the second argument. You can then parse this string or array and print only what you need.

Example which would print all but the first line of output:

exec('cmd.exe /c final.bat',$output);
for($i = 1; $i < count($output); ++$i) echo $output[$i],'<br />';


回答2:

system returns any output from the command, so a simple

echo system(...);

would do the trick. To be able to send text as input to the invoked command, you should look into using popen() instead, which lets you "use" the invoked command as if you were using it from a shell/command prompt.

Note that system will block until the external program finishes. In your case, with it sitting there with "press any key to continue", this will never happen and your PHP process will just sit there indefinitely (or until max_execution_time is exceeded, whichever comes first).