views:

151

answers:

5

Is it possible to simulate mouse's move in PHP ? By that I mean to do something like :

$mouse->moveToCoordinate($x,$Y); // will move the screen to to the coordinate $X, $Y of the screen
$mouse->moveVector($x,$Y); // will move from the current point to the (current X + $X, current Y + $Y); 
$mouse->click(); // will simulate a mouse click on the screen.

This would be usable, even if no browser is open (so cannot use the classic javascript solution).

+4  A: 

PHP is a server side scripting language and cannot do that. You should do that by Javascript. It's possible to do that from PHP (write needed Javascript in PHP and send to client). The most real-time solution is using AJAX but you still suffering round-trip lags depending on client speed.

Xaqron
I know that. PHP can be also used as a CLI, so not necessary from a server. And as I said, I'm searching for a solution that shouldn't use a browser in the first place. Some libraries might have been developed to extend PHP's capabilities.
Cedric
The most confusing concepts for who coded standalone applications when they move to coding web-based applications is the "server-client connectionless round trip". How long do you have been working on web-based applications ?
Xaqron
@Xaqron : more than a year Xaqron.
Cedric
When client sends a request to the server the response (in your case it's a page, not a file) is just a bunch of TEXTs which should be rendered by some application namely BROWSER. After that the PHP code has nothing to do until a new request is received. This new request is from a new client or from the same client (posting a form, requesting the same page, using AJAX on page ...). So it's not like a standalone application which has a real-time connection with the user. Moving the mouse has meaning when the client see the whole page (after it has been generated and transferred to the client).
Xaqron
php -f foo.php bar foo2 . Check http://uk.php.net/manual/en/features.commandline.php . In the question "no browser is open" obviously implies it's not a web app.
Cedric
A: 

+1 To everything that was said before.

I'll add that more details on the goal is needed. Depending on what you really want (A click to do what ? On what ? etc...), you can still use cURL to reach a page, parsing it and following to the link you want (if that's a link you want to click...), entering a whole form and submiting it, etc... You can access to the html code and save it in a file on your server (if that's what you need.) etc... etc...

Anyway, as everyone said, PHP is server-side and, even as CLI, you need to have a server on your localhost and that will just execute a PHP script, PHP that don't have access to mouse/mouse movement etc without a client-side language like javascript.

Chouchenos
Maybe I am wrong, but /usr/bin/php doesn't need a server to be run. It's a stand-alone application. http://php.net/manual/en/install.unix.commandline.php
Cedric
+1  A: 

1 - use exec() and : http://stackoverflow.com/questions/2986286/simulate-mouse-movement-in-ubuntu . Basically, use any other language, compile it if needed, and use the executable with argument throughout command line.

2 - PHP-QT might do the trick

| IT IS POSSIBLE !!! |

People have suggested to use another language (javascript), but for this problem, it's not possible to use a browser. So other languages will do the trick.

Thanks for your message though, and if anybody have other solutions, I'd be interested to know them.

Cedric
And shame on the guy who pressed the button 'this question is unclear or not useful' for my question :D (sorry for this useless comment -:) )
Cedric
@Cedric: Since you are using PHP in quite an unusual way, it would be worthwhile pointing that out in big letters in your question. If someone sees PHP in the title of a question, it's quite understandable that they would think that you are writing a web app, making the question seem unclear.
Douglas
@Douglas: Even if I mention "no browser is open ", excluding all browser oriented based application/ pages, I _thank you_ for your comment as I understand why people overlooked "This would be usable, even if no browser is open (so cannot use the classic javascript solution)."
Cedric
What's the point of using PHP if it's just to launch another executable... ? (maybe just not to learn a much more appropriate client language)
Chouchenos
@Cedric: Something like, "I am writing a PHP application which runs on the client (not on a web server). How do I set the location of the user's mouse pointer?" -- though I'd still expect "Why are you using PHP?" questions, so answering that up front would be a good idea. "I'm using PHP so that I can reuse XYZ library..." or something.
Douglas
@Douglas : Thanks Douglas :D Much appreciated, I'll be more careful next time.
Cedric
As I am more proficient in PHP than in Java, it's much quicker to write the main part of the application in PHP for me, and try to use a trick to do the thing PHP can't do by itself.
Cedric
A: 

Just as an exercise. It might be possible to write standalone desktop PHP app that has access to user pointer. For that you have to use bindings such as http://gtk.php.net/ (there were Qt bindings some time ago, but project seems to be dead). And even that it might be hard. PHP-GTK is not well documented at this moment.

greg
Thanks for GTK suggestion, I haven't thought about that.
Cedric
A: 

IMHO I think your going about whatever it is your trying to do in the wrong way. There is no way to control the users mouse unless your using some sort of remote desktop app as that would be a security issue. That said I could take a guess as some possible things you could do

  1. set focus on an object using javascript
  2. click something using javascript 3 write and applescript (if on a mac) to click something in the finder or automate a process

hth

EDIT is should also be noted that if you use applescript stuidio you have access to objective c which would let you write code to change the mouse position. but I don't recommend it the user should control the mouse and nothing else should

mcgrailm