| 1 |
- {"name":"php-phantomjs","tagline":"Execute PhantomJS commands through PHP","body":"Usage\r\n========\r\n\r\nThis page contains some common examples of how to use the PHP PhantomJS library.\r\n\r\n* [Basic Request](#basic-request)\r\n* [POST Request](#post-request)\r\n* [Other Request Methods](#other-request-methods)\r\n* [Response Data](#response-data)\r\n* [Screen Captures](#screen-captures)\r\n* [Custom Timeout](#custom-timeout)\r\n* [Delay Page Render](#delay-page-render)\r\n* [Custom Run Options](#custom-run-options)\r\n\r\nBasic Request\r\n-------------\r\n\r\nA basic GET request:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest();\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $request->setMethod('GET');\r\n $request->setUrl('http://google.com');\r\n \r\n $client->send($request, $response);\r\n \r\n if($response->getStatus() === 200) {\r\n echo $response->getContent();\r\n }\r\n```\r\n\r\nYou can also set the URL, request method and timeout period when creating a new request instance through the message factory:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest('http://google.com', 'GET', 5000);\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $client->send($request, $response);\r\n \r\n if($response->getStatus() === 200) {\r\n echo $response->getContent();\r\n }\r\n```\r\n\r\nPOST Request\r\n------------\r\n\r\nA basic POST request:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest();\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $data = array(\r\n 'param1' => 'Param 1',\r\n 'param2' => 'Param 2'\r\n );\r\n \r\n $request->setMethod('POST');\r\n $request->setUrl('http://google.com');\r\n $request->setRequestData($data); // Set post data\r\n \r\n $client->send($request, $response);\r\n```\r\n\r\nOther Request Methods\r\n---------------------\r\n\r\nThe PHP PhantomJS library supports the following request methods:\r\n\r\n* OPTIONS\r\n* GET\r\n* HEAD\r\n* POST\r\n* PUT\r\n* DELETE\r\n* PATCH\r\n\r\nThe request method can be set when creating a new request instance through the message factory:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest('http://google.com', 'PUT');\r\n```\r\n\r\nOr on the request instance itself:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest();\r\n $request->setMethod('PATCH');\r\n```\r\n\r\nResponse Data\r\n-------------\r\n\r\nA standard response gives you access to the following interface:\r\n\r\n| Accessor | Description | Return Type |\r\n| :-----------------: | ----------------------------------------------------------------------------------------- | :------------: |\r\n| getHeaders() | Returns an array of all response headers. | Array |\r\n| getHeader(*header*) | Returns the value for a specific response header e.g. Content-Type. | Mixed |\r\n| getStatus() | The response status code e.g. 200. | Int |\r\n| getContent() | The raw page content of the requested page. | String |\r\n| getContentType() | The content type of the requested page. | String |\r\n| getUrl() | The URL of the requested page. | String |\r\n| getRedirectUrl() | If the response was a redirect, this will return the redirect URL. | String |\r\n| isRedirect() | Will return true if the response was a redirect or false otherwise. | Boolean |\r\n| getConsole() | Returns an array of any javascript errors on the requested page along with a stack trace. | Array |\r\n\r\nIf the response contains a status code of 0, chances are the request failed. Check the request [debug log](http://jonnnnyw.github.io/php-phantomjs/debugging.html) for more detailed information about what may have gone wrong.\r\n\r\nScreen Captures\r\n---------------\r\n\r\nYou 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:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $file = '/path/to/save/your/screen/capture/file.jpg';\r\n \r\n $request->setCaptureFile($file);\r\n \r\n $client->send($request, $response);\r\n```\r\n\r\nYou will need to make sure the directory that you are saving the file to exists and is writable by your application.\r\n\r\nYou can also set the width, height, x and y axis for your screen capture:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $file = '/path/to/save/your/screen/capture/file.jpg';\r\n \r\n $top = 10;\r\n $left = 10;\r\n $width = 200;\r\n $height = 400;\r\n \r\n $request->setCaptureFile($file);\r\n $request->setCaptureDimensions($width, $height, $top, $left);\r\n \r\n $client->send($request, $response);\r\n```\r\n\r\nCustom Timeout\r\n--------------\r\n\r\nBy default, each request will timeout after 5 seconds. You can set a custom timeout period (in milliseconds) for each request:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $timeout = 10000; // 10 seconds\r\n \r\n $request->setTimeout($timeout);\r\n \r\n $client->send($request, $response);\r\n```\r\n\r\nDelay Page Render\r\n-----------------\r\n\r\nSometimes 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:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n \r\n $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n \r\n $delay = 5; // 5 seconds\r\n \r\n $request->setDelay($delay);\r\n \r\n $client->send($request, $response);\r\n```\r\n\r\nYou can set a page render delay for standard requests also.\r\n\r\n\r\nCustom Run Options\r\n------------------\r\n\r\nThe 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:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n $client->addOption('--load-images=true');\r\n $client->addOption('--ignore-ssl-errors=true');\r\n \r\n $request = $client->getMessageFactory()->createRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n\r\n $client->send($request, $response);\r\n```\r\n\r\nYou can also set a path to a JSON configuration file that contains multiple PhantomJS options:\r\n\r\n```php\r\n\r\n use JonnyW\\PhantomJs\\Client;\r\n \r\n $client = Client::getInstance();\r\n $client->addOption('--config=/path/to/config.json');\r\n \r\n $request = $client->getMessageFactory()->createRequest('http://google.com');\r\n $response = $client->getMessageFactory()->createResponse();\r\n\r\n $client->send($request, $response);\r\n```\r\n\r\nSee the [PhantomJS Documentation](http://phantomjs.org/api/command-line.html) for a full list of command line options.","google":"","note":"Don't delete this file! It's used internally to help with page regeneration."}
|