19

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?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
MOB
  • 853
  • 2
  • 13
  • 28
  • 1
    https://packagist.org/packages/jonnyw/php-phantomjs – jszobody Nov 25 '13 at 20:17
  • http://stackoverflow.com/questions/10651320/starting-phantomjs-server-from-php-and-waiting-for-its-response (google for "phantomjs php" gives lots of good hits, some even at stackoverflow) – Mörre Nov 25 '13 at 20:17
  • i cant run php phantomjs script on my local and it's make some error that i dont know whay they happen ... i test some this script from git – MOB Nov 25 '13 at 20:21

3 Answers3

29
  • 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');
Benjy
  • 406
  • 4
  • 4
  • 1
    thanx a lot for answer . im try to use it at localhost in windows 7 and xampp server . how i make it executable? and what package i should to download ? windows or linux version ? – MOB Nov 26 '13 at 19:50
  • For anybody having issues with this solution, by following [this answer](https://groups.google.com/d/msg/phantomjs/s8RS-Glf7fg/z82YTZpWaycJ) I managed to successfully call my Phantom script from PHP. – Fisu Mar 20 '15 at 08:18
  • @Benjy I tried this `$response = exec('/phantom/phantomjs myscript.js'); exec("phantom/bin/phantomjs phantom/examples/hello.js", $response); (replace " with normal ticks) echo implode("
    ", $response);` but nothing at all is echoed, why is this? Everything has proper permissions, and it executes with no issue through SSH
    – Ethan Feb 20 '17 at 01:48
  • how to make Phantomjs executable by using chmod +x... i try to run chmod +x command in command prompt but it gives error 'can't open chmod' – Asad Ali Khan Aug 26 '17 at 10:14
11

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.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
4

I recently published a project that gives PHP access to a browser. Get it here: https://github.com/merlinthemagic/MTS. Under the hood it relies on PhantomJS.

After downloading and setup you would simply use the following code:

$myUrl          = "http://www.example.com";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//now you can either retrive the DOM and parse it, like this:
$domData    = $windowObj->getDom();

//or take screen shots
$imageData    = $windowObj->screenshot();

//or use the mouse to click buttons:
$windowObj->mouseEventOnElement("[id=searchInput]", 'leftclick');

//or type with the keyboard :
$windowObj->sendKeyPresses("my search words");

//or load and execute custom javascript, fill forms etc, etc.
MerlinTheMagic
  • 575
  • 5
  • 16
  • I tried MTS in windows 10 and it doesn't work: "Fatal error: Uncaught Exception: MTS\Common\Devices\Actions\Local\Host\OperatingSystem::execute>> Could not determine OS Architecture in..." – Tarilonte Jun 20 '21 at 13:33
  • @Tarilonte SO is not the right venue for troubleshooting, please head over to https://github.com/merlinthemagic/MTS/issues and open a new issue. – MerlinTheMagic Jun 20 '21 at 13:35