troubleshooting.rst 13 KB

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