intro.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Introduction
  2. ============
  3. | PHP PhantomJS is a flexible PHP library to load pages through the
  4. PhantomJS
  5. | headless browser and return the page response. It is handy for testing
  6. | websites that demand javascript support and also supports screen
  7. captures.
  8. Feature List
  9. ------------
  10. - Load webpages through the PhantomJS headless browser
  11. - View detailed response data including page content, headers, status
  12. code etc.
  13. - Handle redirects
  14. - View javascript console errors
  15. - View detailed PhantomJS debuged information
  16. - Save screen captures to local disk
  17. - Define screen capture x, y, width and height parameters
  18. - Set viewport size
  19. - Delay page rendering for a specified time
  20. - Execute PhantomJS with command line options
  21. - Easily build and run custom PhantomJS scripts
  22. Prerequisites
  23. -------------
  24. PHP PhantomJS requires PHP **5.3.0** or greater to run.
  25. Installation
  26. ------------
  27. It is recommended that you use Composer to install PHP PhantomJS. First,
  28. add the following to your project’s ``composer.json`` file:
  29. .. code:: xml
  30. "scripts": {
  31. "post-install-cmd": [
  32. "PhantomInstaller\\Installer::installPhantomJS"
  33. ],
  34. "post-update-cmd": [
  35. "PhantomInstaller\\Installer::installPhantomJS"
  36. ]
  37. }
  38. This will ensure the latest version of PhantomJS is installed for your
  39. system, in your bin folder. If you haven’t defined your bin folder in
  40. your composer.json, add the path:
  41. .. code:: xml
  42. "config": {
  43. "bin-dir": "bin"
  44. }
  45. Finally, install PHP PhantomJS from the root of your project:
  46. .. code:: shell
  47. $ composer require "jonnyw/php-phantomjs:3.*"
  48. If you would like to use another installation method or would like to
  49. see more detailed installation instructions, see the `installation <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/installation.rst>`__
  50. documentation.
  51. Basic Usage
  52. -----------
  53. The following illustrates how to make a basic GET request and output the
  54. page content:
  55. .. code:: php
  56. <?php
  57. use JonnyW\PhantomJs\Client;
  58. $client = Client::getInstance();
  59. /**
  60. * @see JonnyW\PhantomJs\Message\Request
  61. **/
  62. $request = $client->getMessageFactory()->createRequest('http://google.com', 'GET');
  63. /**
  64. * @see JonnyW\PhantomJs\Message\Response
  65. **/
  66. $response = $client->getMessageFactory()->createResponse();
  67. // Send the request
  68. $client->send($request, $response);
  69. if($response->getStatus() === 200) {
  70. // Dump the requested page content
  71. echo $response->getContent();
  72. }
  73. And if you would like to save a screen capture to local disk:
  74. .. code:: php
  75. <?php
  76. use JonnyW\PhantomJs\Client;
  77. $client = Client::getInstance();
  78. /**
  79. * @see JonnyW\PhantomJs\Message\CaptureRequest
  80. **/
  81. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
  82. $request->setCaptureFile('/path/to/save/capture/file.jpg');
  83. /**
  84. * @see JonnyW\PhantomJs\Message\Response
  85. **/
  86. $response = $client->getMessageFactory()->createResponse();
  87. // Send the request
  88. $client->send($request, $response);
  89. For more detailed examples see the `usage`_ section, or to create
  90. your own custom scripts check out the `advanced`_ documentation.
  91. .. _usage: https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/usage.rst
  92. .. _advanced: https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/advanced.rst