--- layout: post title: Usage categories: [] tags: [] fullview: true version: 4.0 --- This page contains some common examples of how to use the PHP PhantomJS library. * [Setup](#setup) * [Basic Request](#basic-request) * [POST Request](#post-request) * [Other Request Methods](#other-request-methods) * [Custom Headers](#custom-headers) * [Response Data](#response-data) * [Screen Captures](#screen-captures) * [Output To PDF](#output-to-pdf) * [Set Viewport Size](#set-viewport-size) * [Set Background Color](#set-background-color) * [Custom Timeout](#custom-timeout) * [Delay Page Render](#delay-page-render) * [On Load Finished](#on-load-finished) * [PhantomJS Options](#phantomjs-options) * [Exceptions](#exceptions) For more advanced customization or to load your own PhantomJS scripts, see the [custom scripts]({{ site.BASE_PATH }}/4.0/4-custom-scripts/) section. --- Setup ----- By default the PhantomJS library will look for the PhantomJS executable in the bin folder relative to where your script is running `~/bin/phantomjs`. If the executable cannot be found or if the path to your PhantomJS executable differs from the default location, for example you have installed PhantomJS globally, you will need to define the path to your PhantomJS executable manually. {% highlight php %} getEngine()->setPath('/path/to/phantomjs'); {% endhighlight %} > #### Note > The path must include the name of the PhantomJS executable in it, not just a path to the directory containing the executable. Basic Request ------------- A basic GET request: {% highlight php %} getMessageFactory()->createRequest(); $response = $client->getMessageFactory()->createResponse(); $request->setMethod('GET'); $request->setUrl('http://jonnyw.me'); $client->send($request, $response); if($response->getStatus() === 200) { echo $response->getContent(); } {% endhighlight %} You can also set the URL, request method and timeout period when creating a new request instance through the message factory: {% highlight php %} getMessageFactory()->createRequest('http://jonnyw.me', 'GET', 5000); ... {% endhighlight %} POST Request ------------ A basic POST request: {% highlight php %} getMessageFactory()->createRequest(); $response = $client->getMessageFactory()->createResponse(); $data = array( 'param1' => 'Param 1', 'param2' => 'Param 2' ); $request->setMethod('POST'); $request->setUrl('http://jonnyw.me'); $request->setRequestData($data); // Set post data $client->send($request, $response); {% endhighlight %} Other Request Methods --------------------- The PHP PhantomJS library supports the following request methods: * OPTIONS * GET * HEAD * POST * PUT * DELETE * PATCH The request method can be set when creating a new request instance through the message factory: {% highlight php %} getMessageFactory()->createRequest('http://jonnyw.me', 'PUT'); {% endhighlight %} Or on the request instance itself: {% highlight php %} getMessageFactory()->createRequest(); $request->setMethod('PATCH'); ... {% endhighlight %} Custom Headers --------------------- Custom headers can be added to the request. {% highlight php %} getMessageFactory()->createRequest(); $request->addHeader('custom_header_key', 'custom_header_value'); {% endhighlight %} For example: when you want to request an authentication protected page with Laravel 5: {% highlight php %} setMethod('POST'); $request->addHeader('X-CSRF-TOKEN', csrf_token()); $request->addHeader('cookie', $laravel_request->header('cookie')); ... {% endhighlight %} Response Data ------------- A standard response object gives you access to the following interface: | Accessor | Description | Return Type | | :-----------------: | ----------------------------------------------------------------------------------------- | :------------: | | getHeaders() | Returns an array of all response headers. | Array | | getHeader(*header*) | Returns the value for a specific response header e.g. Content-Type. | Mixed | | getStatus() | The response status code e.g. 200. | Integer | | getContent() | The raw page content of the requested page. | String | | getContentType() | The content type of the requested page. | String | | getUrl() | The URL of the requested page. | String | | getRedirectUrl() | If the response was a redirect, this will return the redirect URL. | String | | isRedirect() | Will return true if the response was a redirect or false otherwise. | Boolean | | getConsole() | Returns an array of any javascript errors on the requested page along with a stack trace. | Array | > #### Note > If the response ever contains a status code of 0, chances are the request failed. Check the request [debug log]({{ site.BASE_PATH }}/4.0/troubleshooting/#how-do-i-debug-a-request) for more detailed information about what may have gone wrong. Screen Captures --------------- 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: {% highlight php %} getMessageFactory()->createCaptureRequest('http://jonnyw.me'); $response = $client->getMessageFactory()->createResponse(); $file = '/path/to/save/your/screen/capture/file.jpg'; $request->setOutputFile($file); $client->send($request, $response); {% endhighlight %} 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: {% highlight php %} setCaptureDimensions($width, $height, $top, $left); ... {% endhighlight %} > #### Note > Sometimes you may want to wait for all the resources on the page to load before saving a capture to disk. This can be achieved by either [delaying the page render](#delay-page-render) or [waiting for all resources to load](#on-load-finished). Output To PDF ------------- You can output a page to PDF by creating a PDF request and setting the path you wish to save the document to. {% highlight php %} getMessageFactory()->createPdfRequest('http://jonnyw.me'); $response = $client->getMessageFactory()->createResponse(); $file = '/path/to/save/your/pdf/document.pdf'; $request->setOutputFile($file); $client->send($request, $response); {% endhighlight %} You can set an optional repeating header and/or footer to the PDF output. {% highlight php %} setRepeatingHeader('