intro.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. Introduction
  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. First add the following to your composer.json file:
  24. ```xml
  25. "scripts": {
  26. "post-install-cmd": [
  27. "PhantomInstaller\\Installer::installPhantomJS"
  28. ],
  29. "post-update-cmd": [
  30. "PhantomInstaller\\Installer::installPhantomJS"
  31. ]
  32. }
  33. ```
  34. This will ensure the latest version of PhantomJS is install for your system, in your bin folder. If you haven't defined your bin folder in your composer.json, add the path:
  35. ```xml
  36. "config": {
  37. "bin-dir": "path/to/bin/dir"
  38. }
  39. ```
  40. Finally, install PHP PhantomJS from the root of your project:
  41. ```xml
  42. composer require "jonnyw/php-phantomjs:3.*"
  43. ```
  44. If you would like to use another installation method or would like to see more detailed installation instructions, see the [installation](http://jonnnnyw.github.io/php-phantomjs/installation.html) documentation.
  45. Basic Usage
  46. ---------------------
  47. The following illustrates how to make a basic GET request and output the page content:
  48. ```php
  49. <?php
  50. use JonnyW\PhantomJs\Client;
  51. $client = Client::getInstance();
  52. /**
  53. * @see JonnyW\PhantomJs\Message\Request
  54. **/
  55. $request = $client->getMessageFactory()
  56. ->createRequest('http://google.com', 'GET');
  57. /**
  58. * @see JonnyW\PhantomJs\Message\Response
  59. **/
  60. $response = $client->getMessageFactory()->createResponse();
  61. // Send the request
  62. $client->send($request, $response);
  63. if($response->getStatus() === 200) {
  64. // Dump the requested page content
  65. echo $response->getContent();
  66. }
  67. ```
  68. And if you would like to save a screen capture to local disk:
  69. ```php
  70. <?php
  71. use JonnyW\PhantomJs\Client;
  72. $client = Client::getInstance();
  73. /**
  74. * @see JonnyW\PhantomJs\Message\Request
  75. **/
  76. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
  77. $request->setCaptureFile('/path/to/save/capture/file.jpg');
  78. /**
  79. * @see JonnyW\PhantomJs\Message\Response
  80. **/
  81. $response = $client->getMessageFactory()->createResponse();
  82. // Send the request
  83. $client->send($request, $response);
  84. ```
  85. For more detailed examples see the [examples](http://jonnnnyw.github.io/php-phantomjs/examples.html) section, or to create your own custom scripts check out the [advanced](http://jonnnnyw.github.io/php-phantomjs/advanced.html) documentation.