如何在php中模拟浏览器来执行javascript文件?

I have a php file that can generate html or js files that would draw a graph in the browser.

But since this files have to be created by php on server side, I cannot have browser involved. What I want to do, is to be able to emulate the browser and create an image from those files directly from php, in order to email them to the appropriate department.

I think this can be done in the following steps:

  1. Some executable program that can be run from Command Promt with an argument being my file
  2. PHP could send commands to Command Promt telling it to run "the created file" with that program which would generate the image.
  3. Then I would email that image

Example of usability

Lets say you are running extjs web application in your browser, right? There is no user interaction, it might stay running for days. Now without doing anything you want to send an image of your CPU performance graph to your testing department.

Does anyone know the best way of doing this? Thanks

Use server - side graph image generator. You don't need to emulate the browser and then fetch back to server - it's way more complicated and unreliable.

I have used JPGraph (http://jpgraph.net) in few projects - it can generate graph images on server side into some temporary directory, and then you can either include into email or output to client. If you find jpGraph not suitable solution, take a look at alternatives:

But if you still find that fetching javascript output from client and bringing it back to server is your way to go, then you might want to take a look at http://wkhtmltopdf.org/.

I found what I was looking for, even though it is a little late but still...

I am using phantomjs. It is really cool. You can write php file and give it to phantomjs, it will create image or pdf as if you screen capture from browser.

This is what the command looks like from phantomjs directory:

phantomjs "youJavascriptFile" "outputType" "fileName" "file"

Where

  • outputType can be 'letter' or 'width x height'
  • file can be a http path to your server's php file
  • youJavascriptFile is the js file where you use this other inputs and a little phantomjs magic to create an output.