0

I have a PHP page that grabs a variable via GET and then pulls in some information from a database based on that variable. Once finished with the server-side stuff, there is some javascript that runs and takes the data supplied and creates a .png image using a 3rd party API and then saves that image to my server using an AJAX POST call to another PHP page.

That all works fine, but what I'd like to do now is automate some calls to that PHP page. Namely, say I have 100 such variables to go through, I want to, preferably in Java with a for loop, call that PHP page with each variable in turn.

The problem is that client-side javascript. It won't execute with the simple URLConnection in Java. It seems like I need some sort of browser replicator or some way to have java act like it's calling the PHP in a browser?

Alternatively, I could make do with having a third PHP page act in place of the Java as the controller, but I'm faced with the same problem of getting the javascript to execute.

Am I missing something easy? Is this set up not possible? I'd really prefer to do it in Java if possible to fold it into other code I already have running.


Let me try to add some more specifics without bogging it down too much. There's a PHP file getData.php that takes in an ID number via GET. So I call it like ./getData.php?id=someId

That PHP file takes the ID, goes to my DB and retrieves some data and pastes it into the HTML source. Then once the page is finished, I have some javascript within getData.php that retrieves that data, formats it into a DataTable and passes it off to Google Visualization API in order to make a SVG chart.

Then I have more JS that runs that takes that SVG object, turns it into a Canvas object, grabs the base64 image data from it and finally POSTs to saveTo.php with the following array:

{'id' : id, 'data' : imgData}

saveTo.php simply takes in that POST data, creates a file on my server based on id and pastes the imgData into it. The end result is that I can pass in an ID to getData.php and end up with saved image of a Visualization chart that I want made based on data in my DB tied to that ID.

That all works by hand. But I have ~1,000 of these IDs and I'd like to have it so this whole process is run each morning so that I can have updated images based on yesterday's data.


I should mention that I did try using the 3rd party toolkit HtmlUnit (http://htmlunit.sourceforge.net/) but just keep getting these errors:

com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'.

Mattios550
  • 372
  • 1
  • 5
  • 18
user16565
  • 1
  • 2
  • I think your best bet on getting the javascript to run is to do it in the browser. You could pretty easily have the third php page that you talked about create some iframes, or some httprequests that it pushed to the browser. Those requests would in turn go back and hit php page 2. – doliver Apr 13 '13 at 01:01
  • Of course you can, you can use `stream_create_context()` in php to post data to wherever. But it's impossible for us to elaborate more without seeing the ajax call that we need to spoof in php. Also, it would seem as if a generic REST controller would do you well. – Ohgodwhy Apr 13 '13 at 01:05

1 Answers1

0

Some more searching around and hitting upon the right keywords to try finally led me to Selenium, which is exactly what I was looking for.

http://docs.seleniumhq.org/projects/webdriver/

user16565
  • 1
  • 2