|
|
@@ -2,7 +2,8 @@ PHP PhantomJS
|
|
|
=============
|
|
|
|
|
|
PHP PhantomJS is a simple PHP library to load pages through the PhantomJS
|
|
|
-headless browser and return the page response.
|
|
|
+headless browser and return the page response. It is handy for testing
|
|
|
+websites that demand javascript support and also supports screen captures.
|
|
|
|
|
|
|
|
|
0.0 Table of Contents
|
|
|
@@ -27,8 +28,7 @@ The PhantomJS executable comes bundled with the library. If installed
|
|
|
via composer, the file will be symlinked in your bin folder.
|
|
|
|
|
|
If you are looking for a PHP library to run external javascript files
|
|
|
-through PhantomJS then this is not for you. Check out
|
|
|
-[PhantomJS runner](https://github.com/Dachande663/PHP-PhantomJS).
|
|
|
+through PhantomJS then this is not for you. Check out [PhantomJS runner](https://github.com/Dachande663/PHP-PhantomJS).
|
|
|
|
|
|
|
|
|
2.0 Examples
|
|
|
@@ -50,10 +50,9 @@ $response = $client->open('http://www.google.com');
|
|
|
|
|
|
if($response->getStatus() === 200) {
|
|
|
|
|
|
- // Dump the response content
|
|
|
+ // Dump the requested page content
|
|
|
echo $response->getContent();
|
|
|
}
|
|
|
-
|
|
|
```
|
|
|
|
|
|
Load a URL, save a screen capture to provided location and return the response:
|
|
|
@@ -71,7 +70,7 @@ $client = Client::getInstance();
|
|
|
$response = $client->capture('http://www.google.com', '/path/to/save/screen/capture.png');
|
|
|
```
|
|
|
|
|
|
-Handle response redirect:
|
|
|
+Get a response header:
|
|
|
|
|
|
```php
|
|
|
<?php
|
|
|
@@ -85,15 +84,10 @@ $client = Client::getInstance();
|
|
|
**/
|
|
|
$response = $client->open('http://www.google.com');
|
|
|
|
|
|
-if($response->isRedirect()) {
|
|
|
-
|
|
|
- $response = $client->open(
|
|
|
- $response->getRedirectUrl()
|
|
|
- );
|
|
|
-}
|
|
|
+echo $response->getHeader('Cache-Control');
|
|
|
```
|
|
|
|
|
|
-Define a new path to the PhantomJS executable
|
|
|
+Handle response redirect:
|
|
|
|
|
|
```php
|
|
|
<?php
|
|
|
@@ -101,7 +95,6 @@ Define a new path to the PhantomJS executable
|
|
|
use JonnyW\PhantomJs\Client;
|
|
|
|
|
|
$client = Client::getInstance();
|
|
|
-$client->setPhantomJs('/path/to/phantomjs');
|
|
|
|
|
|
/**
|
|
|
* @see JonnyW\PhantomJs\Response
|
|
|
@@ -116,7 +109,7 @@ if($response->isRedirect()) {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-Set timeout for the request (defaults to 5 seconds):
|
|
|
+Define a new path to the PhantomJS executable:
|
|
|
|
|
|
```php
|
|
|
<?php
|
|
|
@@ -124,20 +117,39 @@ Set timeout for the request (defaults to 5 seconds):
|
|
|
use JonnyW\PhantomJs\Client;
|
|
|
|
|
|
$client = Client::getInstance();
|
|
|
-$client->setTimeout(10000); // In milleseconds
|
|
|
+$client->setPhantomJs('/path/to/phantomjs');
|
|
|
+```
|
|
|
|
|
|
-/**
|
|
|
- * @see JonnyW\PhantomJs\Response
|
|
|
- **/
|
|
|
-$response = $client->open('http://www.google.com');
|
|
|
+Set timeout for the request (defaults to 5 seconds):
|
|
|
|
|
|
-if($response->isRedirect()) {
|
|
|
+```php
|
|
|
+<?php
|
|
|
|
|
|
- $response = $client->open(
|
|
|
- $response->getRedirectUrl()
|
|
|
- );
|
|
|
-}
|
|
|
+use JonnyW\PhantomJs\Client;
|
|
|
+
|
|
|
+$client = Client::getInstance();
|
|
|
+$client->setTimeout(10000); // In milleseconds
|
|
|
```
|
|
|
|
|
|
3.0 Troubleshooting
|
|
|
-------------
|
|
|
+------------
|
|
|
+
|
|
|
+Look at the response class (JonnyW\PhantomJs\Response) to see what data you have access to.
|
|
|
+
|
|
|
+An explanation of the errors that are thrown by the client:
|
|
|
+
|
|
|
+### CommandFailedException
|
|
|
+
|
|
|
+The command sent to the PhantomJS executable failed. This should be very rare and is probably my fault if this happens (sorry).
|
|
|
+
|
|
|
+### InvalidUrlException
|
|
|
+
|
|
|
+The URL you are providing is an invalid format. It is very loose verification.
|
|
|
+
|
|
|
+### NoPhantomJsException
|
|
|
+
|
|
|
+The PhantomJS executable cannot be found or it is not executable. Check the path and permissions.
|
|
|
+
|
|
|
+### NotWriteableException
|
|
|
+
|
|
|
+The screen capture location you provided or your /tmp folder are not writeable. The /tmp folder is used to temporarily write the scripts that PhantomJS executes. They are deleted after execution or on failure.
|