layout: post title: Introduction categories: [] tags: [] fullview: true version: 3.0
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.
PHP PhantomJS requires PHP 5.3.0 or greater to run.
It is recommended that you use Composer to install PHP PhantomJS. First, add the following to your project’s composer.json file:
{% highlight yaml %}
#composer.json
"scripts": {
"post-install-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
],
"post-update-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
]
}
{% endhighlight %}
This will ensure the latest version of PhantomJS is installed for your system, in your bin folder. If you haven’t defined your bin folder in your composer.json, add the path:
{% highlight yaml %}
#composer.json
"config": {
"bin-dir": "bin"
}
{% endhighlight %}
Finally, install PHP PhantomJS from the root of your project:
{% highlight bash %}
#bash
$ composer require "jonnyw/php-phantomjs:3.*"
{% endhighlight %}
If you would like to use another installation method or would like to see more detailed installation instructions, see the installation documentation.
The following illustrates how to make a basic GET request and output the page content:
{% highlight php %}
<?php
use JonnyW\PhantomJs\Client;
$client = Client::getInstance();
/**
* @see JonnyW\PhantomJs\Message\Request
**/
$request = $client->getMessageFactory()->createRequest('http://google.com', 'GET');
/**
* @see JonnyW\PhantomJs\Message\Response
**/
$response = $client->getMessageFactory()->createResponse();
// Send the request
$client->send($request, $response);
if($response->getStatus() === 200) {
// Dump the requested page content
echo $response->getContent();
}
{% endhighlight %}
And if you would like to save a screen capture to local disk:
{% highlight php %}
<?php
use JonnyW\PhantomJs\Client;
$client = Client::getInstance();
/**
* @see JonnyW\PhantomJs\Message\CaptureRequest
**/
$request = $client->getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
$request->setCaptureFile('/path/to/save/capture/file.jpg');
/**
* @see JonnyW\PhantomJs\Message\Response
**/
$response = $client->getMessageFactory()->createResponse();
// Send the request
$client->send($request, $response);
{% endhighlight %}
For more detailed examples see the usage section, or to create your own custom scripts check out the advanced documentation.