Is there a headless browser library for PHP? Would like something that has a JS engine built into it. FOSS preferred.

- 3,452
- 1
- 26
- 23

- 39,403
- 111
- 277
- 441
-
PHP really sucks for this task. The only thing close to fully php is Selenium and that's not really the best either since it just manipulates the brwoser and not really headless. – Anthony Hatzopoulos Nov 16 '12 at 22:08
-
What do You need as backend? Fully-transformer DOM tree or graphics? If graphics -- you will not have any 'headless' solution at all: it _will_ have X.org dependencies anyway. – Arenim Nov 22 '12 at 17:46
-
27 years later, now google chrome offers a headless version that you can find a php lib to manipulate it right there: https://github.com/chrome-php/headless-chromium-php – gsouf Apr 27 '18 at 11:33
7 Answers
PhantomJS - http://phantomjs.org/
PhantomJS is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
You can couple it with something like php-PhantomjsRunner (now deprecated) if you want or bake your own.
When setup and ready to start testing with PhantomJS, pick out one of the various Testing Frameworks at your disposal.
I recently also came across this tutorial from tutsplus should it help anyone.

- 1
- 1

- 10,437
- 2
- 40
- 57
-
7
-
1@JoeRegan Updated answer and included some more PHP resources. PHP is just a means to an end and in the end OP needs javascript. A full PHP solution will ultimately suck or just can't be done without calling some other command line program. – Anthony Hatzopoulos Nov 16 '12 at 22:13
-
1@AnthonyHatzopoulos i don't think thats what he is asking. Full PHP solution as far as i can read. – Nov 16 '12 at 23:05
-
@JoeRegan Actually webkit is C and C++. PhantomJS uses JS to run its scripts, and for good reason (you can easily interleave the PhantomJS controlling JS with the running page JS - I have, and it rocks). You could write a small RPC server to "bridge" the PhantomJS running instance with your PHP running application. What more do you want? – oxygen Nov 22 '12 at 22:31
-
1Now phantom is abandoned https://github.com/ariya/phantomjs/issues/15344 – fiorebat Nov 12 '18 at 13:33
Check out mink and it's support for zombie as a back-end.
It looks quite good, and appears to be well supported.
Edit:
Unfortunately, php has no native javascript interpreter; so if you are looking for a pure php implementation including javascript support, currently, you will not find it. For headless with javascript support, you will have to rely on something like node.js or phantomjs on the back-end.
I'm sure you've seen them already, but for other options that are either headed or lack javascript support, Mink's other backends are worth perusing.

- 13,133
- 1
- 30
- 39
You sound like you need http://seleniumhq.org/ which can also be used with PHPUnit to run unit tests.
Also take a look at SimpleTest Browser.

- 55,620
- 80
- 262
- 364
-
http://stackoverflow.com/questions/7525521/whats-the-best-way-to-do-headless-browser-testing-with-php-in-linux-specifical – Anthony Hatzopoulos Nov 16 '12 at 21:44
-
2
-
Your link to the SimpleTest browser is broken, but you can find it here: http://simpletest.sourceforge.net/en/browser_documentation.html – cazort Jun 06 '22 at 16:25
Goutte is a simple headless browser for PHP. https://github.com/fabpot/Goutte
Unfortunately it doesn't support JS.

- 10,641
- 2
- 22
- 41
Well, someone could create a bastard headless web browser running in PHP, complete with Javascript support implemented in PHP!

- 24,226
- 19
- 100
- 173
-
Intriguing, but I would not recommend using something like that yet in a production environment; they say it is in "early alpha". – cazort Jun 06 '22 at 16:26
If you're not limited to PHP, or you're able to use system calls, check out xvfb, it's a virtual frame buffer, and you can use that to run Linux based browsers (firefox) with complete JS functionality. You can even pull in the screen data too, and write it to a file.
I've done this before when gathering specific metrics.

- 3,221
- 3
- 22
- 44
Use the HTMLUnit driver, which is part of the Selenium project. WebDriver is based on Webkit (I believe) and so using HTMLUnit (from Selenium) basically is the Java solution. Then, I believe you can call Java code from PHP, although I am unsure on what the details are of getting that working.

- 28,471
- 61
- 196
- 289