intro.rst 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. - Delay page rendering for a specified time
  19. - Execute PhantomJS with command line options
  20. - Easily build and run custom PhantomJS scripts
  21. Prerequisites
  22. -------------
  23. PHP PhantomJS requires PHP **5.3.0** or greater to run.
  24. Installation
  25. ------------
  26. It is recommended that you use Composer to install PHP PhantomJS. First,
  27. add the following to your project’s ``composer.json`` file:
  28. .. code:: xml
  29. "scripts": {
  30. "post-install-cmd": [
  31. "PhantomInstaller\\Installer::installPhantomJS"
  32. ],
  33. "post-update-cmd": [
  34. "PhantomInstaller\\Installer::installPhantomJS"
  35. ]
  36. }
  37. This will ensure the latest version of PhantomJS is installed for your
  38. system, in your bin folder. If you haven’t defined your bin folder in
  39. your composer.json, add the path:
  40. .. code:: xml
  41. "config": {
  42. "bin-dir": "bin"
  43. }
  44. Finally, install PHP PhantomJS from the root of your project:
  45. .. code:: shell
  46. $ composer require "jonnyw/php-phantomjs:3.*"
  47. If you would like to use another installation method or would like to
  48. see more detailed installation instructions, see the `installation <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/installation.rst>`__
  49. documentation.
  50. Basic Usage
  51. -----------
  52. The following illustrates how to make a basic GET request and output the
  53. page content:
  54. .. code:: php
  55. <?php
  56. use JonnyW\PhantomJs\Client;
  57. $client = Client::getInstance();
  58. /**
  59. * @see JonnyW\PhantomJs\Message\Request
  60. **/
  61. $request = $client->getMessageFactory()->createRequest('http://google.com', 'GET');
  62. /**
  63. * @see JonnyW\PhantomJs\Message\Response
  64. **/
  65. $response = $client->getMessageFactory()->createResponse();
  66. // Send the request
  67. $client->send($request, $response);
  68. if($response->getStatus() === 200) {
  69. // Dump the requested page content
  70. echo $response->getContent();
  71. }
  72. And if you would like to save a screen capture to local disk:
  73. .. code:: php
  74. <?php
  75. use JonnyW\PhantomJs\Client;
  76. $client = Client::getInstance();
  77. /**
  78. * @see JonnyW\PhantomJs\Message\CaptureRequest
  79. **/
  80. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com', 'GET');
  81. $request->setCaptureFile('/path/to/save/capture/file.jpg');
  82. /**
  83. * @see JonnyW\PhantomJs\Message\Response
  84. **/
  85. $response = $client->getMessageFactory()->createResponse();
  86. // Send the request
  87. $client->send($request, $response);
  88. For more detailed examples see the `usage`_ section, or to create
  89. your own custom scripts check out the `advanced`_ documentation.
  90. .. _usage: https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/usage.rst
  91. .. _advanced: https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/advanced.rst