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?

3
  • 1
    packagist.org/packages/jonnyw/php-phantomjs Commented Nov 25, 2013 at 20:17
  • stackoverflow.com/questions/10651320/… (google for "phantomjs php" gives lots of good hits, some even at stackoverflow) Commented Nov 25, 2013 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 Commented Nov 25, 2013 at 20:21

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

4 Comments

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 ?
For anybody having issues with this solution, by following this answer I managed to successfully call my Phantom script from PHP.
@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("<br>", $response); but nothing at all is echoed, why is this? Everything has proper permissions, and it executes with no issue through SSH
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'
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.

2 Comments

What's the unresolved issue, is it that it's ridiculously slow to open webpages post 1.9?
Is this stackoverflow.com/q/61983687/3170029 possible using PHP PhantomJS?
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.

2 Comments

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 SO is not the right venue for troubleshooting, please head over to github.com/merlinthemagic/MTS/issues and open a new issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.