usage.rst 9.9 KB

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