tags:

views:

54

answers:

2

Hello,

In regards to Can a PHP Proxy call Javascript functions like a click I'm investigating a solution that involves using PHP's HTTP class to simulate a client's browser.

Does anyone know if the PHP HTTP class supports javascript functions? Can you recommend any others that do?

Thanks!

A: 

An Http stream handler is not in any way related to a browser. It will not generate a DOM tree, block ads or execute javascript - it is just a library handling the http protocol.

soulmerge
+2  A: 

No, it does not. You have to implement this yourself. If you want to have it all-serverside, look into phpjs, j4p5 or better yet the spidermonkey pecl extension. http://pecl.php.net/package/spidermonkey

A better alternative might be to script a real browser yourself via an XPI plugin / XPCOM bridge. There once was an extension called "JSSh", and another "SD connector", http://www.activestate.com/blog/2008/05/jssh-replacement-sd-connector

Neither of these options is simple. That's why nobody has done it, and why such extravagant features are specifically not in the HTTP class.

mario
Thanks for the info. I like the idea of embedding spidermonkey in PHP and will definitely look into that further. However, I want to make sure that I asked my question correctly: are you saying that if a requested a page via php's HTTP class that had a javascript function that fires "onClick" nested in the response page to some GET call or something, that I would still have to use the spidermonkey in PHP method? All of this would have to go on server side, because the proxy cannot reveal the page to the client.
gnucom
Yes, you need to instantiate Spidermonkey and set up a browser environment (I don't believe it brings a ready-to-use document tree built in). Then parse(!) the proxied html page for JS hooks, and fake the onClick or onMouseOver or onWhatever event. This is, I believe, unmanageable in a generic way. I've never heard of a proxy doing such elaborate simulations. And it seems unpromising to try it in PHP. At best, you could emulate the onClick behaviour for *very specific* pages, if you reimplement the requestflow on a case-by-case basis.
mario
Thanks Mario. It literally is just one page I need this behavior on, so this is a contrived example. The problem I'm trying to overcome is a viewstate variable that an IIS server is using to authenticate my requests. Unfortunately when I post the values directly I get a corrupted viewstate, so I'm thinking that using the javascript callback function to log in will be the only way. Thanks a lot for your help.
gnucom
Writing some website scrapers myself, you should look into Firebug/Dragonfly. Maybe you missed something. It's sometimes essential to replicate browser fingerprints exactly (Keep-Alive, Accept-Encoding, etc..). If it's really an AJAX call which triggers the autorization, you must emulate `X-Requested-With: XMLHttpRequest`. There's nothing else the Javascript code might spoof.
mario
Mario - you were right. I didn't rebuild the headers exactly as they were being sent. I had to add the Keep-Alive, Accept-Encoding, etc...) as you mentioned and it suddenly worked! Thanks again!
gnucom