troubleshooting.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. Troubleshooting
  2. ===============
  3. - `It's not installing anything to my bin
  4. directory <#its-not-installing-anything-to-my-bin-directory>`__
  5. - `I am getting a InvalidExecutableException when making a
  6. request <#i-am-getting-a-invalidexecutableexception-when-making-a-request>`__
  7. - `I am getting a NotWritableException when making a
  8. request <#i-am-getting-a-notwritableexception-when-making-a-request>`__
  9. - `Why do I need the phantomloader
  10. file? <#why-do-i-need-the-phantomloader-file>`__
  11. - `Why am I getting a status code of 0 in the
  12. response? <#why-am-i-getting-a-status-code-of-0-in-the-response>`__
  13. - `It's not saving my screenshots <#its-not-saving-my-screenshots>`__
  14. - `Can I set the screenshot size? <#can-i-set-the-screenshot-size>`__
  15. - `Can I set the viewport size? <#can-i-set-the-viewport-size>`__
  16. - `How to I debug a request? <#how-to-i-debug-a-request>`__
  17. - `I am getting SyntaxError: Parse error in the debug
  18. log <#i-am-getting-syntaxerror-parse-error-in-the-debug-log>`__
  19. It's not installing anything to my bin directory
  20. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. When installing via composer, as outlined in the `installation
  22. guide <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/installation.html>`__, it is recommended
  23. that you define the location of the bin folder for your project. This
  24. can be done by adding the following to your ``composer.json`` file:
  25. .. code:: yaml
  26. #composer.json
  27. {
  28. "config": {
  29. "bin-dir": "/path/to/your/projects/bin/dir"
  30. }
  31. }
  32. You need to make sure that this directory exists and is writable before
  33. you install your composer depedencies.
  34. Once you have defined your bin folder in your ``composer.json`` file,
  35. run composer install from the root of your project:
  36. .. code:: shell
  37. #bash
  38. $ php composer.phar install
  39. This should install 2 files to your bin folder: ``phantomjs`` and
  40. ``phantomloader``. If you do not have these 2 files in your bin folder
  41. then check that the folder is writable and run composer install again.
  42. .. important::
  43. If you do not define a bin directory in your ``composer.json`` file
  44. then the default install location will be ``vendor/bin/``. If you
  45. choose to not set a bin folder path then you will need to make sure
  46. that this path is set in the client by calling
  47. ``$client->setBinDir('vendor/bin');`` before making a request.
  48. I am getting a ``InvalidExecutableException`` when making a request
  49. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50. If you have installed via composer, as outlined in the `installation
  51. guide <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/installation.html>`__, then you should
  52. have 2 files installed in either the ``bin`` or ``vendor/bin/``
  53. directory, in the root of your project. These files are called
  54. ``phantomjs`` and ``phantomloader``.
  55. Check that these files exist and are executable by your application. If
  56. they do not exist, see `It's not installing anything to my bin
  57. directory <#its-not-installing-anything-to-my-bin-directory>`__.
  58. If the PHP PhantomJS library cannot locate either of these files then it
  59. will throw a ``InvalidExecutableException``. The message in the
  60. exception should tell you which one it can't execute. If both of these
  61. files exist and they are executable by your application, you may need to
  62. set the path to the directory that these files live in before making a
  63. request:
  64. .. code:: php
  65. <?php
  66. use JonnyW\PhantomJs\Client;
  67. $client = Client::getInstance();
  68. $client->setBinDir('/path/to/bin/dir');
  69. I am getting a ``NotWritableException`` when making a request
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. When making a request, the PHP PhantomJS library compiles a temporary
  72. script file to your system's tmp folder. The location of this folder is
  73. determined through the use of the ``sys_get_temp_dir()`` function. If
  74. this directory is not writable by your application then you will receive
  75. this exception.
  76. Why do I need the ``phantomloader`` file?
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. A proxy loader file is used get around a quirk that PhantomJS has when
  79. it encounters a syntax error in a script file. By default, if PhantomJS
  80. encounters a syntax error when loading a script file it will not exit
  81. execution. This means that PHP PhantomJS will continue to wait for a
  82. response from PhantomJS until PHP reaches its script execution timeout
  83. period. This also means you won't get any debug information informing
  84. you that the PhantomJS script has a syntax error.
  85. To get around this, script files are loaded through a loader script.
  86. This handles the event of a syntax error and ensures that PhantomJS
  87. exits when an error is encountered.
  88. The ``phantomloader`` file is required in order for the PHP PhantomJS
  89. library to run so please make sure that it was installed to your bin
  90. folder and is readable by your application.
  91. Another reason for getting this exception is when you are trying to save
  92. screenshots. See `It's not saving my
  93. screenshots <#its-not-saving-my-screenshots>`__.
  94. Why am I getting a status code of 0 in the response?
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. A status code of 0 in the response object generally means the request
  97. did not complete successfully. This could mean that the URL you are
  98. requesting did not return a response or that something happened when
  99. making the request that did not raise an error in the PHP PhantomJS
  100. library.
  101. Becuase and exception was not thrown, chances are the issue is with
  102. PhantomJS itself or at the endpoint you are calling.
  103. One possible reason for this is that your request has timed out before a
  104. response was returned from the endpoint that you are requesting.
  105. Depending on what you are requesting, some websites seem to take a while
  106. to return a response. An example of this is
  107. `myspace.com <https://myspace.com/>`__ which, at the time of writing
  108. this, takes a considerable amount of time resolve through PhantomJS.
  109. To work around this you can try increasing the timeout period in the PHP
  110. PhantomJS client:
  111. .. code:: php
  112. <?php
  113. use JonnyW\PhantomJs\Client;
  114. $client = Client::getInstance();
  115. $request = $client->getMessageFactory()->createRequest('http://google.com');
  116. $response = $client->getMessageFactory()->createResponse();
  117. $timeout = 20000; // 20 seconds
  118. $request->setTimeout($timeout);
  119. $client->send($request, $response);
  120. If you are still having a problem then you should enable debugging,
  121. before you make the request, and check the debug log. This contains a
  122. dump of information from PhantomJS which could help to track down why
  123. you are not getting a response.
  124. .. code:: php
  125. <?php
  126. use JonnyW\PhantomJs\Client;
  127. $client = Client::getInstance();
  128. $client->debug(true); // Set debug flag
  129. $request = $client->getMessageFactory()->createRequest('http://google.com');
  130. $response = $client->getMessageFactory()->createResponse();
  131. $client->send($request, $response);
  132. echo $client->getLog(); // Output log
  133. You can also try running a test script through your PhantomJS
  134. executable, from the command line, to see if you get a valid response
  135. back. Save the following script somewhere:
  136. .. code:: javascript
  137. //test-script.js
  138. var page = require('webpage').create(),
  139. url = 'YOUR REQUEST URL', // Change this to the URL you want to request
  140. response;
  141. page.onResourceReceived = function (r) {
  142. response = r;
  143. };
  144. phantom.onError = function(msg, trace) {
  145. console.log(msg);
  146. console.log(trace);
  147. phantom.exit(1);
  148. };
  149. page.open (url, 'GET', '', function (status) {
  150. console.log(status);
  151. console.log(JSON.stringify(response));
  152. phantom.exit();
  153. });
  154. And then, assuming you have saved the script above to ``test-script.js``
  155. in the root of your project and your PhantomJS executable is located at
  156. ``bin/phantomjs``, run the following:
  157. .. code:: shell
  158. #bash
  159. $ bin/phantomjs ./test-script.js
  160. You should see an output of the response from PhantomJS:
  161. .. code:: shell
  162. #bash
  163. success
  164. {"contentType":"text/javascript; charset=UTF-8", "headers": ...
  165. If you don't see ``success`` followed by a JSON encoded response object
  166. then there is something the with the URL you are requesting or your
  167. PhantomJS executable. Try reinstalling PhantomJS. If you see ``fail``
  168. instead of ``success``, chances are the URL you are requesting is
  169. invalid or not resolving.
  170. It's not saving my screenshots
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. When making a capture request you need to make sure that you are setting
  173. the file location that you want the screenshot saved to, including the
  174. filename:
  175. .. code:: php
  176. <?php
  177. use JonnyW\PhantomJs\Client;
  178. $client = Client::getInstance();
  179. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');
  180. $response = $client->getMessageFactory()->createResponse();
  181. $file = '/path/to/save/your/screen/capture/file.jpg';
  182. $request->setCaptureFile($file); // Set the capture file
  183. $client->send($request, $response);
  184. The file itself does not need to exist but the parent directory must
  185. exist and be writable by your application. Check that your application
  186. has permissions to write files to the directory you are setting for your
  187. screen captures.
  188. Can I set the screenshot size?
  189. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190. Yes, you can set the width and height of your capture along with the x
  191. and y coordinates of where the capture should start from:
  192. .. code:: php
  193. <?php
  194. use JonnyW\PhantomJs\Client;
  195. $client = Client::getInstance();
  196. $request = $client->getMessageFactory()->createCaptureRequest('http://google.com');
  197. $response = $client->getMessageFactory()->createResponse();
  198. $file = '/path/to/save/your/screen/capture/file.jpg';
  199. $top = 10;
  200. $left = 10;
  201. $width = 200;
  202. $height = 400;
  203. $request->setCaptureFile($file);
  204. $request->setCaptureDimensions($width, $height, $top, $left);
  205. $client->send($request, $response);
  206. Can I set the viewport size?
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. Yes, you can set the viewport dimensions on both regular and capture
  209. requests:
  210. .. code:: php
  211. <?php
  212. use JonnyW\PhantomJs\Client;
  213. $client = Client::getInstance();
  214. $request = $client->getMessageFactory()->createRequest('http://google.com');
  215. $response = $client->getMessageFactory()->createResponse();
  216. $width = 200;
  217. $height = 400;
  218. $request->setViewportSize($width, $height); // Set viewport size
  219. $client->send($request, $response);
  220. How to I debug a request?
  221. ~~~~~~~~~~~~~~~~~~~~~~~~~
  222. By setting the debug flag to ``true`` on the client, you can get a dump
  223. of information output from PhantomJS along with some info events added
  224. by the PHP PhantomJS library:
  225. .. code:: php
  226. <?php
  227. use JonnyW\PhantomJs\Client;
  228. $client = Client::getInstance();
  229. $client->debug(true); // Set debug flag
  230. $request = $client->getMessageFactory()->createRequest('http://google.com');
  231. $response = $client->getMessageFactory()->createResponse();
  232. $client->send($request, $response);
  233. echo $client->getLog(); // Output log
  234. You can also get any javacript console errors along with a stack trace
  235. from the URL you are calling, in the response object:
  236. .. code:: php
  237. <?php
  238. use JonnyW\PhantomJs\Client;
  239. $client = Client::getInstance();
  240. $request = $client->getMessageFactory()->createRequest('http://google.com');
  241. $response = $client->getMessageFactory()->createResponse();
  242. $client->send($request, $response);
  243. var_dump($response->getConsole()); // Outputs array of console errors and stack trace
  244. I am getting ``SyntaxError: Parse error`` in the debug log
  245. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  246. You will only get this error if the script file that is being run by
  247. PhantomJS has a syntax error. If you are writing your own `custom
  248. scripts <https://github.com/jonnnnyw/php-phantomjs/blob/master/doc/advanced.html#custom-phantomjs-scripts>`__
  249. then try setting the `debug flag <#how-to-i-debug-a-request>`__ which
  250. *should* print some more detailed information in the debug log. Also
  251. check that you aren't setting any parameters to ``null`` in your request
  252. object as this could be causing a javascript error due to javascript
  253. variables being set to nothing e.g. ``var width = ,``.