usage.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Usage
  2. =====
  3. This page contains some common examples of how to use the PHP PhantomJS
  4. library.
  5. - `Basic Request <#basic-request>`__
  6. - `POST Request <#post-request>`__
  7. - `Other Request Methods <#other-request-methods>`__
  8. - `Response Data <#response-data>`__
  9. - `Screen Captures <#screen-captures>`__
  10. - `Set Viewport Size <#set-viewport-size>`__
  11. - `Custom Timeout <#custom-timeout>`__
  12. - `Delay Page Render <#delay-page-render>`__
  13. - `Custom Run Options <#custom-run-options>`__
  14. Basic Request
  15. -------------
  16. A basic GET request:
  17. .. code:: php
  18. use JonnyW\PhantomJs\Client;
  19. $client = Client::getInstance();
  20. $request = $client->getMessageFactory()->createRequest();
  21. $response = $client->getMessageFactory()->createResponse();
  22. $request->setMethod('GET');
  23. $request->setUrl('http://google.com');
  24. $client->send($request, $response);
  25. if($response->getStatus() === 200) {
  26. echo $response->getContent();
  27. }
  28. You can also set the URL, request method and timeout period when
  29. creating a new request instance through the message factory:
  30. .. code:: php
  31. use JonnyW\PhantomJs\Client;
  32. $client = Client::getInstance();
  33. $request = $client->getMessageFactory()->createRequest('http://google.com', 'GET', 5000);
  34. $response = $client->getMessageFactory()->createResponse();
  35. $client->send($request, $response);
  36. if($response->getStatus() === 200) {
  37. echo $response->getContent();
  38. }
  39. POST Request
  40. ------------
  41. A basic POST request:
  42. .. code:: php
  43. use JonnyW\PhantomJs\Client;
  44. $client = Client::getInstance();
  45. $request = $client->getMessageFactory()->createRequest();
  46. $response = $client->getMessageFactory()->createResponse();
  47. $data = array(
  48. 'param1' => 'Param 1',
  49. 'param2' => 'Param 2'
  50. );
  51. $request->setMethod('POST');
  52. $request->setUrl('http://google.com');
  53. $request->setRequestData($data); // Set post data
  54. $client->send($request, $response);
  55. Other Request Methods
  56. ---------------------
  57. The PHP PhantomJS library supports the following request methods:
  58. - OPTIONS
  59. - GET
  60. - HEAD
  61. - POST
  62. - PUT
  63. - DELETE
  64. - PATCH
  65. The request method can be set when creating a new request instance
  66. through the message factory:
  67. .. code:: php
  68. use JonnyW\PhantomJs\Client;
  69. $client = Client::getInstance();
  70. $request = $client->getMessageFactory()->createRequest('http://google.com', 'PUT');
  71. Or on the request instance itself:
  72. .. code:: php
  73. use JonnyW\PhantomJs\Client;
  74. $client = Client::getInstance();
  75. $request = $client->getMessageFactory()->createRequest();
  76. $request->setMethod('PATCH');
  77. Response Data
  78. -------------
  79. A standard response gives you access to the following interface:
  80. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  81. | Accessor | Description | Return Type |
  82. +=========================+=============================================================================================+===============+
  83. | getHeaders() | Returns an array of all response headers. | Array |
  84. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  85. | getHeader(\ *header*) | Returns the value for a specific response header e.g. Content-Type. | Mixed |
  86. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  87. | getStatus() | The response status code e.g. 200. | Int |
  88. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  89. | getContent() | The raw page content of the requested page. | String |
  90. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  91. | getContentType() | The content type of the requested page. | String |
  92. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  93. | getUrl() | The URL of the requested page. | String |
  94. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  95. | getRedirectUrl() | If the response was a redirect, this will return the redirect URL. | String |
  96. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  97. | isRedirect() | Will return true if the response was a redirect or false otherwise. | Boolean |
  98. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  99. | getConsole() | Returns an array of any javascript errors on the requested page along with a stack trace. | Array |
  100. +-------------------------+---------------------------------------------------------------------------------------------+---------------+
  101. If the response contains a status code of 0, chances are the request
  102. failed. Check the request `debug
  103. log <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/troubleshooting.rst#how-do-i-debug-a-request>`__
  104. for more detailed information about what may have gone wrong.
  105. Screen Captures
  106. ---------------
  107. You can save screen captures of a page to your local disk by creating a
  108. screen capture request and setting the path you wish to save the file
  109. to:
  110. .. code:: php
  111. use JonnyW\PhantomJs\Client;
  112. $client = Client::getInstance();
  113. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');
  114. $response = $client->getMessageFactory()->createResponse();
  115. $file = '/path/to/save/your/screen/capture/file.jpg';
  116. $request->setCaptureFile($file);
  117. $client->send($request, $response);
  118. You will need to make sure the directory that you are saving the file to
  119. exists and is writable by your application.
  120. You can also set the width, height, x and y axis for your screen
  121. capture:
  122. .. code:: php
  123. use JonnyW\PhantomJs\Client;
  124. $client = Client::getInstance();
  125. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');
  126. $response = $client->getMessageFactory()->createResponse();
  127. $file = '/path/to/save/your/screen/capture/file.jpg';
  128. $top = 10;
  129. $left = 10;
  130. $width = 200;
  131. $height = 400;
  132. $request->setCaptureFile($file);
  133. $request->setCaptureDimensions($width, $height, $top, $left);
  134. $client->send($request, $response);
  135. Set Viewport Size
  136. -----------------
  137. You can easily set the viewport size for a request:
  138. .. code:: php
  139. <?php
  140. use JonnyW\PhantomJs\Client;
  141. $client = Client::getInstance();
  142. $request = $client->getMessageFactory()->createRequest('http://google.com');
  143. $response = $client->getMessageFactory()->createResponse();
  144. $width = 200;
  145. $height = 400;
  146. $request->setViewportSize($width, $height);
  147. $client->send($request, $response);
  148. Custom Timeout
  149. --------------
  150. By default, each request will timeout after 5 seconds. You can set a
  151. custom timeout period (in milliseconds) for each request:
  152. .. code:: php
  153. use JonnyW\PhantomJs\Client;
  154. $client = Client::getInstance();
  155. $request = $client->getMessageFactory()->createRequest('http://google.com');
  156. $response = $client->getMessageFactory()->createResponse();
  157. $timeout = 10000; // 10 seconds
  158. $request->setTimeout($timeout);
  159. $client->send($request, $response);
  160. Delay Page Render
  161. -----------------
  162. Sometimes when taking screen captures you may want to wait until the
  163. page is completely loaded before saving the capture. In this instance
  164. you can set a page render delay (in seconds) for the request:
  165. .. code:: php
  166. use JonnyW\PhantomJs\Client;
  167. $client = Client::getInstance();
  168. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');
  169. $response = $client->getMessageFactory()->createResponse();
  170. $delay = 5; // 5 seconds
  171. $request->setDelay($delay);
  172. $client->send($request, $response);
  173. You can set a page render delay for standard requests also.
  174. Custom Run Options
  175. ------------------
  176. The PhantomJS API contains a range of command line options that can be
  177. passed when executing the PhantomJS executable. These can also be passed
  178. in via the client before a request:
  179. .. code:: php
  180. use JonnyW\PhantomJs\Client;
  181. $client = Client::getInstance();
  182. $client->addOption('--load-images=true');
  183. $client->addOption('--ignore-ssl-errors=true');
  184. $request = $client->getMessageFactory()->createRequest('http://google.com');
  185. $response = $client->getMessageFactory()->createResponse();
  186. $client->send($request, $response);
  187. You can also set a path to a JSON configuration file that contains
  188. multiple PhantomJS options:
  189. .. code:: php
  190. use JonnyW\PhantomJs\Client;
  191. $client = Client::getInstance();
  192. $client->addOption('--config=/path/to/config.json');
  193. $request = $client->getMessageFactory()->createRequest('http://google.com');
  194. $response = $client->getMessageFactory()->createResponse();
  195. $client->send($request, $response);
  196. See the `PhantomJS
  197. Documentation <http://phantomjs.org/api/command-line.html>`__ for a full
  198. list of command line options.