Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Execute PhantomJS from PHP

I want to execute PhantomJS from PHP on localhost.

Can any body explain how to execute PhantomJS from PHP and what package I should download from phantomjs.org?

like image 638
MOB Avatar asked Nov 25 '13 20:11

MOB


People also ask

How do I run PhantomJS?

Go to the “bin” folder and check phantomjs.exe file. If you are using it on a Windows OS, then you can set the path variable under the environment variable for fast access through command prompt. The command to run the PhantomJS program: C:\> phantomjs [options] file.

What PhantomJS 2?

PhantomJS is a headless web browser scriptable with JavaScript. It runs on Windows, macOS, Linux, and FreeBSD. Using QtWebKit as the back-end, it offers fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.


2 Answers

  • download the PhantomJS binary, upload it somewhere and make it executable (chmod +x)
  • if you are going to make screenshots, setup fontconfig (this is pretty specific to my config but the goal is to make sure to have at least some fonts on your system)
  • run the following in PHP:
    $response = exec('/path/to/phantomjs myscript.js');
like image 156
Benjy Avatar answered Oct 12 '22 22:10

Benjy


There's actually a library called PHP PhantomJS, intended to make this easier for you!

PHP PhantomJS is a flexible PHP library to load pages through the PhantomJS headless browser and return the page response. It is handy for testing websites that demand javascript support and also supports screen captures.

Full documentation

Feature list :

  • Load webpages through the PhantomJS headless browser
  • View detailed response data including page content, headers, status code etc.
  • Handle redirects
  • View javascript console errors
  • View detailed PhantomJS debugged information
  • Save screen captures to local disk
  • Set viewport size
  • Define screen capture x, y, width and height parameters
  • Delay page rendering for a specified time
  • Execute PhantomJS with command line options
  • Easily build and run custom PhantomJS scripts

Make sure, though, that your version of PhantomJS is compatible with your version of PHP PhantomJS:

Please Note: Version 4.0 of this library is currently waiting on an unresolved issue with PhantomJS 2.0.

like image 28
John Slegers Avatar answered Oct 13 '22 00:10

John Slegers