Can PHP be used to control a GUI application?
Suppose I have a GUI-only application that runs on Windows and I'd like to create a web interface for it using PHP. Is it even possible? I know I can use functions like system() or exec() to launch programs, but can I have more control over a running GUI app? Primarily, I would like to be able to send it keystrokes, so I can use it to process a file, save the output, and ex开发者_如何转开发it.
Please point me in the right direction.
Does it have to be PHP? You can distribute your GUI application on the web using VNC's Java applet (free), Windows Terminal Services, Citrix and the like (not free). I can not think of a way to make this possible in PHP, not with it acutally "clicking" the GUI.
You might want to look into php-gtk
Can't say I've tried this myself, but you should have a look at PHP's Windows-only extensions, specifically, the COM extension (http://www.php.net/manual/en/refs.utilspec.windows.php).
From the intro in the documentation:
COM is one of the main ways to glue applications and components together on the Windows platform; using COM you can launch Microsoft Word, fill in a document template and save the result as a Word document and send it to a visitor of your web site.
You'll be limited to running scripts on your own box, but that sounds like it's what you'd need. The COM/OLE stuff can be a pretty deep rabbit hole, so good luck!
You can definitely do it, but it would take a bit of hackery. One way to do it would be to write a Windows command-line app that would send button clicks and such to the Windows GUI app. You then call this command-line app using PHP and the exec
and system
calls.
For example, if you were passing it say, Firefox, you would have a firefox-wrapper
app that could be called like this:
firefox-wrapper open-url http://stackoverflow.com
firefox-wrapper press-back
...
I don't think it's possible to actually send mouse clicks or keystrokes to a desktop app via a PHP web app (if it IS possible, I doubt it's easy - or pretty). What might work is to take the functionality that the PHP app needs and move it to a separate library or service that the PHP app can call using a set of methods you expose to it.
The most important thing you need to remember here is the old programming adage that programming languages are analogous to tools. You wouldn't try to remove a flat tire with a hammer, nor would you drive nails with a tire iron. The point: Use the right tool for the job. Using php to do what you plan on doing is like trying to drive a nail with a screwdriver. Find a good quality hammer.
Sounds very possible... Let the GUI application start up a local server, say at 127.0.0.1:[anyportnumber] Then whenever you do something via the web, let the PHP application send commands to your application by sending data to the GUI app that sits there listening to whatever commands it needs to react on.
If you need to see the GUI running in your web application, the VNC viewer applet is the way to go.
If you are looking to provide a web form and manipulate your GUI behind the scenes, check out Windows Automation Tools on your favorite search engine. They have powerful scripting languages that will allow you to create complex (if needed) routines. You can invoke many of them from a command line and pass a script to execute. If you were doing this with PHP, you could even generate the script as a temp file and pass that off to the macro processor.
You might be able to get what you need from Windows Script Host (WSH), which is like batch file scripting, but with more features.
精彩评论