installation.rst 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. Installation
  2. =============
  3. PHP PhantomJS is a flexible PHP library to load pages through the PhantomJS
  4. headless browser and return the page response. It is handy for testing
  5. websites that demand javascript support and also supports screen captures.
  6. Feature List
  7. ---------------------
  8. * Load webpages through the PhantomJS headless browser
  9. * View detailed response data including page content, headers, status code etc.
  10. * Handle redirects
  11. * View javascript console errors
  12. * View detailed PhantomJS debuged information
  13. * Save screen captures to local disk
  14. * Define screen capture x, y, width and height parameters
  15. * Delay page rendering for a specified time
  16. * Execute PhantomJS with command line options
  17. * Easily build and run custom PhantomJS scripts
  18. Prerequisites
  19. ---------------------
  20. PHP PhantomJS requires PHP **5.3.0** or greater to run.
  21. Installation
  22. ---------------------
  23. It is recommended that you use Composer to install PHP PhantomJS:
  24. ```xml
  25. composer require "jonnyw/php-phantomjs:3.*"
  26. ```
  27. If you would like to use another installation method or would like to see more detailed installation instruction, see the [installation](http://jonnnnyw.github.io/php-phantomjs/) documentation.
  28. Basic Usage
  29. ---------------------
  30. The following illustrates how to make a basic GET request and output the page content:
  31. ```php
  32. <?php
  33. use JonnyW\PhantomJs\Client;
  34. $client = Client::getInstance();
  35. /**
  36. * @see JonnyW\PhantomJs\Message\Request
  37. **/
  38. $request = $client->getMessageFactory()
  39. ->createRequest('http://google.com', 'GET');
  40. /**
  41. * @see JonnyW\PhantomJs\Message\Response
  42. **/
  43. $response = $client->getMessageFactory()->createResponse();
  44. // Send the request
  45. $client->send($request, $response);
  46. if($response->getStatus() === 200) {
  47. // Dump the requested page content
  48. echo $response->getContent();
  49. }
  50. ```
  51. And if you would like to save a screen capture to local disk:
  52. ```php
  53. <?php
  54. use JonnyW\PhantomJs\Client;
  55. $client = Client::getInstance();
  56. /**
  57. * @see JonnyW\PhantomJs\Message\Request
  58. **/
  59. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
  60. $request->setCaptureFile('/path/to/save/capture/file.jpg');
  61. /**
  62. * @see JonnyW\PhantomJs\Message\Response
  63. **/
  64. $response = $client->getMessageFactory()->createResponse();
  65. // Send the request
  66. $client->send($request, $response);
  67. ```
  68. For more detailed examples see the [examples](http://jonnnnyw.github.io/php-phantomjs/) section, otherwise to create your own custom scripts check out the [advanced](http://jonnnnyw.github.io/php-phantomjs/) documentation.