advanced.rst 13 KB

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