This page contains some common examples of how to use the PHP PhantomJS library.
For more advanced customization or to load your own PhantomJS scripts, see the advanced documentation.
A basic GET request:
You can also set the URL, request method and timeout period when creating a new request instance through the message factory:
A basic POST request:
Other Request Methods
The PHP PhantomJS library supports the following request methods:
The request method can be set when creating a new request instance through the message factory:
Or on the request instance itself:
A standard response gives you access to the following interface:
||Returns an array of all response headers.
||Returns the value for a specific response header e.g. Content-Type.
||The response status code e.g. 200.
||The raw page content of the requested page.
||The content type of the requested page.
||The URL of the requested page.
||If the response was a redirect, this will return the redirect URL.
||Will return true if the response was a redirect or false otherwise.
If the response contains a status code of 0, chances are the request failed. Check the request debug log for more detailed information about what may have gone wrong.
You can save screen captures of a page to your local disk by creating a screen capture request and setting the path you wish to save the file to:
You will need to make sure the directory that you are saving the file to exists and is writable by your application.
You can also set the width, height, x and y axis for your screen capture:
Set Viewport Size
You can easily set the viewport size for a request:
By default, each request will timeout after 5 seconds. You can set a custom timeout period (in milliseconds) for each request:
Delay Page Render
Sometimes when taking screen captures you may want to wait until the page is completely loaded before saving the capture. In this instance you can set a page render delay (in seconds) for the request:
You can set a page render delay for standard requests also.
Custom Run Options
The PhantomJS API contains a range of command line options that can be passed when executing the PhantomJS executable. These can also be passed in via the client before a request:
You can also set a path to a JSON configuration file that contains multiple PhantomJS options:
See the PhantomJS Documentation for a full list of command line options.