advanced.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. Advanced
  2. ========
  3. - `PhantomJS command line options <#phantomjs-command-line-options>`__
  4. - `Custom PhantomJS scripts <#custom-phantom-js-scripts>`__
  5. - `Writing a custom script <#writing-a-custom-script>`__
  6. - `Using custom request parameters in your
  7. script <#using-custom-request-parameters-in-your-script>`__
  8. - `Loading your script <#loading-your-script>`__
  9. PhantomJS command line options
  10. ------------------------------
  11. The PhantomJS API contains a range of command line options that can be
  12. passed when executing the PhantomJS executable. These can also be passed
  13. in via the client before a request:
  14. .. code:: php
  15. <?php
  16. use JonnyW\PhantomJs\Client;
  17. $client = Client::getInstance();
  18. $client->addOption('--load-images=true');
  19. $client->addOption('--ignore-ssl-errors=true');
  20. $request = $client->getMessageFactory()->createRequest('http://google.com');
  21. $response = $client->getMessageFactory()->createResponse();
  22. $client->send($request, $response);
  23. You can also set a path to a JSON configuration file that contains
  24. multiple PhantomJS options:
  25. .. code:: php
  26. <?php
  27. use JonnyW\PhantomJs\Client;
  28. $client = Client::getInstance();
  29. $client->addOption('--config=/path/to/config.json');
  30. $request = $client->getMessageFactory()->createRequest('http://google.com');
  31. $response = $client->getMessageFactory()->createResponse();
  32. $client->send($request, $response);
  33. See the `PhantomJS
  34. Documentation <http://phantomjs.org/api/command-line.html>`__ for a full
  35. list of command line options.
  36. Custom PhantomJS scripts
  37. ------------------------
  38. In most instances you shouldn't need to worry about the javascript files
  39. that run the PHP PhantomJS library but there may be times when you want
  40. to execute your own custom PhantomJS scripts through the client. This
  41. can be easily achieved by using the built in script loader.
  42. Script files or 'procedures' as they are referred to in the application
  43. are closely mapped to requests. When you create a default request
  44. instance, you are essentially running the default javascript procedure
  45. that comes bundled with the application. When you create a capture
  46. request you are running the capture procedure.
  47. .. code:: php
  48. <?php
  49. use JonnyW\PhantomJs\Client;
  50. $client->getMessageFactory()->createRequest(); // ~/Resources/procedures/default.proc
  51. $client->getMessageFactory()->createCaptureRequest(); // ~/Resources/procedures/capture.proc
  52. Writing a custom script
  53. ~~~~~~~~~~~~~~~~~~~~~~~
  54. The first step in creating your script is to create a procedure file
  55. somewhere. For the purpose of this guide we will refer to it as
  56. ``my_procedure.proc`` but in reality it can be called anything you like.
  57. The only requirement is that the file extension must be ``.proc``.
  58. Create the file somewhere and make sure it can be read by your
  59. application. Make a note of the path to the directory where your file is
  60. created as you will need this when loading your script which is
  61. explained later in this guide.
  62. .. code:: shell
  63. $ touch my_procedure.proc
  64. $ chmod 755 my_procedure.proc
  65. Next open your procedure file in your text editor and write your
  66. PhantomJS script. The `PhantomJS
  67. documentation <http://phantomjs.org/quick-start.html>`__ has more
  68. detailed information on writing custom scripts.
  69. .. code:: javascript
  70. // my_procedure.proc
  71. var page = require('webpage').create();
  72. page.open ('{{ request.getUrl() }}', '{{ request.getMethod() }}', '{{ request.getBody() }}', function (status) {
  73. // It is important that you exit PhantomJS
  74. // when your script has run or when you
  75. // encounter an error
  76. phantom.exit(1);
  77. });
  78. ...
  79. Important
  80. ^^^^^^^^^
  81. | Make sure that ``phantom.exit(1);`` is always called after your
  82. | script has run or if you encounter an error. This requires you to
  83. | take care when handling PhantomJS errors to ensure that you exit the
  84. | PhantomJS script, whether the script was successfully executed or
  85. | not. If you do not call ``phantom.exit(1);`` then PhantomJS will
  86. | continue to run until your PHP script times out. If you find that
  87. | your custom script is hanging then this is most likely the cause.
  88. It is a good practice to create a global error handler in your script
  89. that exits PhantomJS:
  90. .. code:: javascript
  91. // my_procedure.proc
  92. phantom.onError = function(msg, trace) {
  93. phantom.exit(1);
  94. };
  95. ...
  96. Using custom request parameters in your script
  97. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. Before a procedure is executed by the application it is parsed through a
  99. template parser. The PHP PhantomJS library uses the popular `Twig
  100. templating engine <https://github.com/fabpot/Twig>`__. This gives you
  101. access to all the `Twig
  102. niceness <http://twig.sensiolabs.org/doc/templates.html>`__ which you
  103. can use in your custom scripts.
  104. You may have noticed in the example above that we have used some Twig
  105. template tags referencing a request object e.g.
  106. ``{{ request.getUrl() }}``. This is in fact the PHP request instance
  107. that you created and passed to the client when sending your request,
  108. which is injected into the Twig template parser. As a result you gain
  109. full access to all the data contained within the request instance, via
  110. the data accessor methods.
  111. A default request instance contains the following accessors:
  112. +--------------------------+-----------------------------------------------+------------------------------------+
  113. | Accessor | Description | Twig example |
  114. +==========================+===============================================+====================================+
  115. | getMethod() | The request method e.g. GET. | {{ request.getMethod() }} |
  116. +--------------------------+-----------------------------------------------+------------------------------------+
  117. | getTimeout() | The request timeout period in milliseconds. | {{ request.getTimeout() }} |
  118. +--------------------------+-----------------------------------------------+------------------------------------+
  119. | getDelay() | The page render delay in seconds. | {{ request.getDelay() }} |
  120. +--------------------------+-----------------------------------------------+------------------------------------+
  121. | getUrl() | The request URL. | {{ request.getUrl() }} |
  122. +--------------------------+-----------------------------------------------+------------------------------------+
  123. | getBody() | The request body (POST, PUT). | {{ request.getBody() }} |
  124. +--------------------------+-----------------------------------------------+------------------------------------+
  125. | getHeaders(\ *format*) | The request headers. | {{ request.getHeaders('json') }} |
  126. +--------------------------+-----------------------------------------------+------------------------------------+
  127. A capture request contains a few additional ones:
  128. +--------------------+-------------------------------------------+----------------------------------+
  129. | Accessor | Description | Twig example |
  130. +====================+===========================================+==================================+
  131. | getRectTop() | The x coordinate of the capture region. | {{ request.getRectTop() }} |
  132. +--------------------+-------------------------------------------+----------------------------------+
  133. | getRectLeft() | The y coordinate of the capture region. | {{ request.getRectLeft() }} |
  134. +--------------------+-------------------------------------------+----------------------------------+
  135. | getRectWidth() | The width of the capture region. | {{ request.getRectWidth() }} |
  136. +--------------------+-------------------------------------------+----------------------------------+
  137. | getRectHeight() | The height of the capture region. | {{ request.getRectHeight() }} |
  138. +--------------------+-------------------------------------------+----------------------------------+
  139. | getCaptureFile() | The file to save the capture to. | {{ request.getCaptureFile() }} |
  140. +--------------------+-------------------------------------------+----------------------------------+
  141. If you would like to inject additional data into your script through
  142. custom accessors, simply extend the request class with your own:
  143. .. code:: php
  144. <?php
  145. use JonnyW\PhantomJs\Message\Request;
  146. class CustomRequest extends Request
  147. {
  148. public function getSomething()
  149. {
  150. return 'Something!';
  151. }
  152. }
  153. Now you will be able to access the data in your custom script when using
  154. your custom request:
  155. .. code:: javascript
  156. // my_procedure.proc
  157. var something = '{{ request.getSomething() }}'; // Get something
  158. ...
  159. And to use your custom request simply create a new instance of it and
  160. pass it to the client:
  161. .. code:: php
  162. <?php
  163. use JonnyW\PhantomJs\Client;
  164. $client = Client::getInstance();
  165. $response = $client->getMessageFactory()->createResponse();
  166. $request = new CustomRequest();
  167. $request->setMethod('GET');
  168. $request->setUrl('http://www.google.com');
  169. $client->send($request, $response);
  170. Loading your script
  171. ~~~~~~~~~~~~~~~~~~~
  172. Now that you have your custom script and you've added your custom
  173. request parameters, you may be wondering how to tell the client to
  174. actually load your script. This is done by creating a procedure loader
  175. and telling it where to find your script files.
  176. The service container has a factory that makes creating a new procedure
  177. loader easy:
  178. .. code:: php
  179. <?php
  180. use JonnyW\PhantomJs\Client;
  181. use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;
  182. $location = '/path/to/your/procedure/directory';
  183. $serviceContainer = ServiceContainer::getInstance();
  184. $procedureLoader = $serviceContainer->get('procedure_loader_factory')
  185. ->createProcedureLoader($location);
  186. ...
  187. The client contains a chain procedure loader which lets you set multiple
  188. loaders at the same time. Ultimately this means that you can load your
  189. custom scripts while still maintaining the ability to load the default
  190. scripts if you choose.
  191. Now add your procedure loader to the chain loader:
  192. .. code:: php
  193. <?php
  194. ...
  195. $client = Client::getInstance();
  196. $client->getProcedureLoader()->addLoader($procedureLoader);
  197. ...
  198. The last thing you need to do is to tell the request which script you
  199. want to load for that request. This is done by setting the request type
  200. to the name of your procedure file, minus the extension:
  201. .. code:: php
  202. <?php
  203. ...
  204. $request = $client->getMessageFactory()->createRequest();
  205. $request->setType('my_procedure');
  206. ...
  207. Or if you are using a custom request as outlined in the `custom request
  208. parameters <#using-custom-request-parameters-in-your-script>`__ section,
  209. you can implement a ``getType()`` method which returns the name of your
  210. procedure, eliminating the need to set the request type for each
  211. request:
  212. .. code:: php
  213. <?php
  214. use JonnyW\PhantomJs\Message\Request;
  215. class CustomRequest extends Request
  216. {
  217. public function getType()
  218. {
  219. return 'my_procedure';
  220. }
  221. }
  222. Below is a full example for clarity:
  223. .. code:: php
  224. <?php
  225. use JonnyW\PhantomJs\Client;
  226. use JonnyW\PhantomJs\DependencyInjection\ServiceContainer;
  227. $location = '/path/to/your/procedure/directory';
  228. $serviceContainer = ServiceContainer::getInstance();
  229. $procedureLoader = $serviceContainer->get('procedure_loader_factory')
  230. ->createProcedureLoader($location);
  231. $client = Client::getInstance();
  232. $client->getProcedureLoader()->addLoader($procedureLoader);
  233. $request = $client->getMessageFactory()->createRequest();
  234. $request->setType('my_procedure');
  235. $response = $client->getMessageFactory()->createResponse();
  236. $client->send($request, $response);
  237. Troubleshooting
  238. ^^^^^^^^^^^^^^^
  239. | If you find that your script isn't running or that you are receiving
  240. | a status of '0' back in the response, chances are you have a syntax
  241. | error in you script. It pays to turn debugging on in the client
  242. | ``$client->debug(true)`` which will then give you access to some log
  243. | information through ``$client->getLog()``.
  244. See more detailed information about
  245. `debugging <{{%20site.BASE_PATH%20}}/debugging.html>`__.