simulate mouse move in php? - php

⚠ In this question, PHP is used in an unusual way. It is not used as a server side language ("no browser is open"). It is intended to be run on my own computer, simulating mouse move on my computer.
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 should be usable, even if no browser is open (so cannot use the classic browser-side javascript solution).

1 - use exec() and : 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.

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.

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.

+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.

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
set focus on an object using javascript
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

It's not so hard. Look example.
You can easily edit it and send AJAX HTTP request for x,y positions and return xstart->x , ystart->y.
Hard part is make object to avoid other objects.

Related

PHP include file based on screen size

<?php
include 'components/server.php';
Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?
While technically possible, it's absolutely not the best way of doing things.
Why?
Because PHP runs on the server and only the output of that PHP execution is given to the browser. You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.
If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.
If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".
Alternative
It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.

Retrieve fully rendered page using Curl, or other means?

Is there a way to retrieve the fully rendered html from a page with javascript post rendering ? If I use curl, it simply retrieves the base html, but lacks the post rendering of iframes, javascript processing etc.
What would be the best way to accomplish this?
As no-one else has answered (except the copmment above, but I'll come to that later) I'll try to help as much as possible.
There no "simple" answer. PHP can't process javascript/navigate the DOM natively, so you need something that can.
Your options as I see it:
If you are after screen grab (which is what I'm hoping as you also want Flash to load), I suggest you use one of the commercial APIs that are out there for doing this. You can find some in this list http://www.programmableweb.com/apitag/?q=thumbnail, for example http://www.programmableweb.com/api/convertapi-web2image
Otherwise you need to run something yourself that can handle Javascript and the DOM on, orconnected to, your server. For this, you'd need an automated browser that you can run serverside and get the information you need. Follow the list in Bergi's comment above and you'd need to test a suitable solution - the main one Selinium is great for "unit testing" on a known website, but I'm not sure on how I'd script it to handle random sites, for example. As you would (presumably) only have one "automated browser" and you don't know how long each page will take to load, you'd need to queue the requests and handle one at a time. You'd also need to ensure pop-up alert()s are handled, all the third party libraries (you say you want flash?!) installed, handle redirects, timeouts and potential memory hogs (if running this non-stop, you'll periodically want to kill your browser and restart it to clean out the memory!). Also handle virus attacks, pop-up windows and requests to close the browser completely.
Thirdly, VB has a web-browser component. I used it for a project a long time ago to do something similarish, but on a known site. Whether it's possible with .NET (to me, it' a huge security risk), and how you program for unknowns (e.g. pop-ups and Flash) I have no idea. But if you're desparate an adventurous .NET developer may be able to suggest more.
In summary - if you want more than a screen grab and can choose option 1, good luck ;)
If you're looking for something scriptable with no GUI you could use a headless browser. I've used PhantomJS for similar tasks.
If still relevant, I found that the easy way to this is using PhantomJs as a Service;
public string GetPagePhantomJs(string url)
{
using (var client = new System.Net.Http.HttpClient())
{
client.DefaultRequestHeaders.ExpectContinue = false;
var pageRequestJson = new System.Net.Http.StringContent(#"{'url':'" + url + "','renderType':'plainText','outputAsJson':false }");
var response = client.PostAsync("https://PhantomJsCloud.com/api/browser/v2/SECRET_KEY/", pageRequestJson).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
It is really simple, when subscribing to the service there is a free plan that allows 500 pages/day. The SECRET_KEY is to be replaced by your own key that you will get.
Use a "terminal" browser like w3m or lynx. Even if the site you want to access needs login, this is possible, for example:
curl [-u login:pass] http://www.a_page.com | w3m -T text/html -dump
or
curl [-u login:pass] http://www.a_page.com | lynx -stdin -dump
This should give you the whole html with all frames etc.
look at this command line IECapt.exe
It has no javascript support, but lynx was useful for me in a situation where I needed to do processing of data from a webpage. This way I got the (plaintext) rendering and didn't have to filter through the raw html tags as with curl.
lynx -nonumbers -dump -width=9999999 ${url} | grep ... et cetera.

PHP Print code (print into paper)

Is there a PHP function to print something from a web page (inside the browser)?
I was searching in Google but the result is the print() function. Or any ideas, or any web programming language that you can share. I need a code to print a list of subjects from my database. Sorry for this stupid question. I don't know if that is possible.
If you are talking about a web application, you can bring up the print dialog box with JavaScript using the window.print() function.
Without using activex or other such trickery, there is no way to automatically send something to the printer.
This is a good thing, imagine a malicious web site printing thousands of pages on your machine without your consent.
If you are running a command line php that will depend on the operating system.
If the intention is a print preview, where the user can click an “Print this” button and the server will print the document you can use CUPS if you use a Linux/Apple/BSD server. http://www.cups.org/articles.php?L545+TNews+Q
try this, you need to add php_printer.dll, then enable that extension (after restarting apache)
http://in2.php.net/manual/en/function.printer-write.php

how do you take a snapshot of your current browser window using php

I've tried searching everywhere but there's seems to be no implementation available other than having the client use a file (batch/exe of some sort).
You just can't do it. PHP is server side scripting language, maybe you can do that using JavaScript, but I'm not even sure about that.
I know someone implemented such service, but actually he had to use Mozilla browser, which opened, a script (I think it was not JS, maybe perl, c/c++) made a screenshot and uploaded it.
I'm assuming you mean "your" in the general sense. If you mean "how does one take a screenshot...", you generally hit the print screen key. If you're trying to capture your users' browser output, I'd say that it's probably not possible. If it were, the best you could get is the output of what you wrote yourself.
Google Gears might be hackable to do something close, if you can simulate the print screen key press with JS and get the file to save somewhere gears can access.
You can't do that in PHP, as PHP is running on the server, and not the client.
To get screenshots of the browser, you can take a look at, for instance, this list.
If you are look for an automated solution to take screenshot of web pages opened in a browser window, you could also look at this question : How to capture x screen using PHP, shell_exe and scrot and it's answers.
And, finally, and without selecting any particular post, you can try a search on SO ; something like screenshot browser, sorted by relevance, seems to get some interesting posts :-)
Good luck !

How to invoke js without displaying the code

I was wondering, I want to plant a JS tracking code (analytics) in a few websites to track their traffic. But I don't want that when viewing the site's source code people will be able to see that I've embedded the JS tracking code there.
Is it possible? Maybe by using an Apache/PHP trick?
Thanks,
Roy.
Nope it's not possible, for the browser to execute any code at least some of it must be initially visible, even if that code is to then retrieve the tracking code itself.
In addition all the modern web developer tools provide access to any code that is loaded so anyone can use those to see anything you've attempted to load discretely.
The more important question is why you want to hide that you're tracking people?
It's not absolutely possible with Javascript. Javascript always runs in the context of the user's browser, so it always means that the user will have access to see the script. You can obfuscate it, or try some tricks similar to anti-hotlinking on the JS code, but it will still be relatively easy for someone to figure out what the code does with a simple tool like Firebug.
You can, however, track your traffic without JavaScript. Analytics uses JavaScript for portability, and because some of the data it accesses can only be accessed with JavaScript. However, there are more passive ways of tracking your traffic which don't require JavaScript, such as any log analyzer like AWstats. You just don't get some of the cool features of Analytics.
It's not possible, but you could just name your script file something innocent like "mouseover.js".
It's not possible: JS code has to be run by the web browser, which means that -- which ever way you try put it -- it has to be readable by the browser and thus by anyone that inspects the page.
You could try obfuscating the JS, but that won't stop anyone that is determined to see what's happening.
You could ask yourself what the odds are that more than a few people will check whether you're tracking them -- I wouldn't expect it.
You can't technically hide the code... But you can scramble it so it's not readable to anybody. I used http://hivelogic.com/enkoder/form by Dan Benjamin to scrable some JS on my page (in this case I scrambled my email address). It scrambles it so the browser can execute it, but it's not humanly readable...
Then you can just call it as a function like I did in from this script http://www.jamischarles.com/css_js/email_encoder.js. Give it a try.

Categories