index.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Troubleshooting</title>
  6. <meta name="author" content="Jonny W">
  7. <!-- Enable responsive viewport -->
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
  10. <!--[if lt IE 9]>
  11. <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  12. <![endif]-->
  13. <!-- Le styles -->
  14. <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  15. <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  16. <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/syntax/syntax.css" rel="stylesheet">
  17. <link href="http://jonnnnyw.github.io/php-phantomjs/assets/css/style.css" rel="stylesheet">
  18. <!-- Le fav and touch icons -->
  19. <!-- Update these with your own images
  20. <link rel="shortcut icon" href="images/favicon.ico">
  21. <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
  22. <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
  23. <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
  24. -->
  25. <link rel="alternate" type="application/rss+xml" title="" href="http://jonnnnyw.github.io/php-phantomjs/feed.xml">
  26. <script>
  27. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  28. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  29. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  30. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  31. ga('create', 'UA-53289013-1', 'auto');
  32. ga('send', 'pageview');
  33. </script>
  34. </head>
  35. <body>
  36. <nav class="navbar navbar-default visible-xs" role="navigation" id="global-nav">
  37. <!-- Brand and toggle get grouped for better mobile display -->
  38. <div class="navbar-header">
  39. <button type="button" class="navbar-toggle" data-parent="#global-nav" data-toggle="collapse" data-target="#navbar-collapse-main">
  40. <span class="sr-only">Toggle navigation</span>
  41. <span class="icon-bar"></span>
  42. <span class="icon-bar"></span>
  43. <span class="icon-bar"></span>
  44. </button>
  45. <button type="button" class="navbar-toggle version-toggle" data-parent="#global-nav" data-toggle="collapse" data-target="#navbar-collapse-version">
  46. <span class="sr-only">Toggle navigation</span>
  47. <span class="fa fa-terminal"><b>3.0</b> version</</span>
  48. </button>
  49. <a class="navbar-brand" href="http://jonnnnyw.github.io/php-phantomjs/">
  50. <img src="http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs-small.png" class="img-circle" />
  51. PHP PhantomJs
  52. </a>
  53. </div>
  54. <div class="panel">
  55. <!-- Collect the nav links, forms, and other content for toggling -->
  56. <div class="collapse navbar-collapse" id="navbar-collapse-version">
  57. <ul class="nav navbar-nav">
  58. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/" title="Version 4.0" class="active"><b>4.0</b> version</a></li>
  59. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/" title="Version 3.0"><b>3.0</b> version</a></li>
  60. </ul>
  61. </div><!-- /.navbar-collapse -->
  62. </div>
  63. <div class="panel">
  64. <!-- Collect the nav links, forms, and other content for toggling -->
  65. <div class="collapse navbar-collapse" id="navbar-collapse-main">
  66. <ul class="nav navbar-nav">
  67. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/">Introduction</a></li>
  68. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/2-installation/">Installation</a></li>
  69. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/3-usage/">Usage</a></li>
  70. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/4-advanced/">Advanced Usage</a></li>
  71. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/5-troubleshooting/">Troubleshooting</a></li>
  72. </ul>
  73. </div><!-- /.navbar-collapse -->
  74. </div>
  75. </nav>
  76. <!-- version-menu-dropdown -->
  77. <div class="btn-group hidden-xs" id="nav-version">
  78. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  79. <i class="fa fa-terminal"><b>3.0</b> version</i>
  80. </button>
  81. <ul class="dropdown-menu" role="menu">
  82. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/" title="4.0 version" class="active"><b>4.0</b> version</a></li>
  83. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/" title="3.0 version"><b>3.0</b> version</a></li>
  84. <li class="divider"></li>
  85. </ul>
  86. </div>
  87. <!-- nav-menu-dropdown -->
  88. <div class="btn-group hidden-xs" id="nav-menu">
  89. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  90. <i class="fa fa-bars"></i>
  91. </button>
  92. <ul class="dropdown-menu" role="menu">
  93. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/"><i class="fa fa-folder"></i>Introduction</a></li>
  94. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/2-installation/"><i class="fa fa-folder"></i>Installation</a></li>
  95. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/3-usage/"><i class="fa fa-folder"></i>Usage</a></li>
  96. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/4-advanced/"><i class="fa fa-folder"></i>Advanced Usage</a></li>
  97. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/5-troubleshooting/"><i class="fa fa-folder"></i>Troubleshooting</a></li>
  98. <li class="divider"></li>
  99. <li><a href="#"><i class="fa fa-arrow-up"></i>Top of Page</a></li>
  100. </ul>
  101. </div>
  102. <div class="col-sm-3 sidebar hidden-xs">
  103. <! -- sidebar.html -->
  104. <header class="sidebar-header" role="banner">
  105. <a href="http://jonnnnyw.github.io/php-phantomjs/">
  106. <img src="http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs.png" class="img-circle" />
  107. </a>
  108. <h3 class="title">
  109. <a href="http://jonnnnyw.github.io/php-phantomjs/">PHP PhantomJs</a>
  110. </h3>
  111. </header>
  112. <div id="bio" class="text-center">
  113. Run PhantomJS scripts through PHP
  114. </div>
  115. <div id="contact-list" class="text-center">
  116. <ul class="list-unstyled list-inline">
  117. <li>
  118. <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=watch&count=true"
  119. allowtransparency="true" frameborder="0" scrolling="0" width="85" height="20"></iframe>
  120. </li>
  121. <li>
  122. <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=fork&count=true"
  123. allowtransparency="true" frameborder="0" scrolling="0" width="83" height="20"></iframe>
  124. </li>
  125. </ul>
  126. </div>
  127. <! -- sidebar.html end -->
  128. </div>
  129. <p id="version-notice">You are currently viewing version <span>3.0</span> documentation.</p>
  130. <div class="col-sm-9 col-sm-offset-3">
  131. <div class="page-header">
  132. <h1>Troubleshooting </h1>
  133. </div>
  134. <article>
  135. <div class="col-sm-10">
  136. <div class="article_body">
  137. <ul>
  138. <li><a href="#its-not-installing-anything-to-my-bin-directory">It’s not installing anything to my bin directory</a></li>
  139. <li><a href="#i-am-getting-a-invalidexecutableexception-when-making-a-request">I am getting a <code>InvalidExecutableException</code> when making a request</a></li>
  140. <li><a href="#i-am-getting-a-notwritableexception-when-making-a-request">I am getting a <code>NotWritableException</code> when making a request</a></li>
  141. <li><a href="#why-do-i-need-the-phantomloader-file">Why do I need the <code>phantomloader</code> file?</a></li>
  142. <li><a href="#why-am-i-getting-a-status-code-of-0-in-the-response">Why am I getting a status code of 0 in the response?</a></li>
  143. <li><a href="#its-not-saving-my-screenshots">It’s not saving my screenshots</a></li>
  144. <li><a href="#can-i-set-the-screenshot-size">Can I set the screenshot size?</a></li>
  145. <li><a href="#can-i-set-the-viewport-size">Can I set the viewport size?</a></li>
  146. <li><a href="#how-do-i-debug-a-request">How do I debug a request?</a></li>
  147. <li><a href="#i-am-getting-syntaxerror-parse-error-in-the-debug-log">I am getting <code>SyntaxError: Parse error</code> in the debug log</a></li>
  148. </ul>
  149. <hr />
  150. <h4 id="its-not-installing-anything-to-my-bin-directory">It’s not installing anything to my bin directory</h4>
  151. <p>When installing via composer, as outlined in the <a href="http://jonnnnyw.github.io/php-phantomjs/installation.html">installation guide</a>, it is recommended that you define the location of the bin folder for your project. This can be done by adding the following to your <code>composer.json</code> file:</p>
  152. <figure class="highlight"><pre><code class="language-yaml" data-lang="yaml"> <span class="c1">#composer.json</span>
  153. <span class="p-Indicator">{</span>
  154. <span class="s">&quot;config&quot;</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span>
  155. <span class="s">&quot;bin-dir&quot;</span><span class="p-Indicator">:</span> <span class="s">&quot;/path/to/your/projects/bin/dir&quot;</span>
  156. <span class="p-Indicator">}</span>
  157. <span class="p-Indicator">}</span></code></pre></figure>
  158. <p>You need to make sure that this directory exists and is writable before you install your composer depedencies.</p>
  159. <p>Once you have defined your bin folder in your <code>composer.json</code> file, run composer install from the root of your project:</p>
  160. <figure class="highlight"><pre><code class="language-bash" data-lang="bash">
  161. <span class="c">#bash</span>
  162. <span class="nv">$ </span>php composer.phar install</code></pre></figure>
  163. <p>This should install 2 files to your bin folder: <code>phantomjs</code> and <code>phantomloader</code>. If you do not have these 2 files in your bin folder then check that the folder is writable and run composer install again.</p>
  164. <blockquote>
  165. <h4 id="note">Note</h4>
  166. <p>If you do not define a bin directory in your <code>composer.json</code> file then the default install location will be <code>vendor/bin/</code>. If you choose to not set a bin folder path then you will need to make sure that this path is set in the client by calling <code>$client-&gt;setBinDir('vendor/bin');</code> before making a request.</p>
  167. </blockquote>
  168. <h4 id="i-am-getting-a-invalidexecutableexception-when-making-a-request">I am getting a <code>InvalidExecutableException</code> when making a request</h4>
  169. <p>If you have installed via composer, as outlined in the <a href="http://jonnnnyw.github.io/php-phantomjs/installation.html">installation guide</a>, then you should have 2 files installed in either the <code>bin</code> or <code>vendor/bin/</code> directory, in the root of your project. These files are called <code>phantomjs</code> and <code>phantomloader</code>. </p>
  170. <p>Check that these files exist and are executable by your application. If they do not exist, see <a href="#its-not-installing-anything-to-my-bin-directory">It’s not installing anything to my bin directory</a>.</p>
  171. <p>If the PHP PhantomJS library cannot locate either of these files then it will throw a <code>InvalidExecutableException</code>. The message in the exception should tell you which one it can’t execute. If both of these files exist and they are executable by your application, you may need to set the path to the directory that these files live in before making a request:</p>
  172. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span>
  173. <span class="x"> </span><span class="cp">&lt;?php</span>
  174. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  175. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  176. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">setBinDir</span><span class="p">(</span><span class="s1">&#39;/path/to/bin/dir&#39;</span><span class="p">);</span></code></pre></figure>
  177. <h4 id="i-am-getting-a-notwritableexception-when-making-a-request">I am getting a <code>NotWritableException</code> when making a request</h4>
  178. <p>When making a request, the PHP PhantomJS library compiles a temporary script file to your system’s tmp folder. The location of this folder is determined through the use of the <code>sys_get_temp_dir()</code> function. If this directory is not writable by your application then you will receive this exception.</p>
  179. <h4 id="why-do-i-need-the-phantomloader-file">Why do I need the <code>phantomloader</code> file?</h4>
  180. <p>A proxy loader file is used get around a quirk that PhantomJS has when it encounters a syntax error in a script file. By default, if PhantomJS encounters a syntax error when loading a script file it will not exit execution. This means that PHP PhantomJS will continue to wait for a response from PhantomJS until PHP reaches its script execution timeout period. This also means you won’t get any debug information informing you that the PhantomJS script has a syntax error.</p>
  181. <p>To get around this, script files are loaded through a loader script. This handles the event of a syntax error and ensures that PhantomJS exits when an error is encountered.</p>
  182. <p>The <code>phantomloader</code> file is required in order for the PHP PhantomJS library to run so please make sure that it was installed to your bin folder and is readable by your application.</p>
  183. <p>Another reason for getting this exception is when you are trying to save screenshots. See <a href="#its-not-saving-my-screenshots">It’s not saving my screenshots</a>.</p>
  184. <h4 id="why-am-i-getting-a-status-code-of-0-in-the-response">Why am I getting a status code of 0 in the response?</h4>
  185. <p>A status code of 0 in the response object generally means the request did not complete successfully. This could mean that the URL you are requesting did not return a response or that something happened when making the request that did not raise an error in the PHP PhantomJS library.</p>
  186. <p>Becuase and exception was not thrown, chances are the issue is with PhantomJS itself or at the endpoint you are calling.</p>
  187. <p>One possible reason for this is that your request has timed out before a response was returned from the endpoint that you are requesting. Depending on what you are requesting, some websites seem to take a while to return a response. An example of this is <a href="https://myspace.com/">myspace.com</a> which, at the time of writing this, takes a considerable amount of time resolve through PhantomJS.</p>
  188. <p>To work around this you can try increasing the timeout period in the PHP PhantomJS client:</p>
  189. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  190. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  191. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  192. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  193. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  194. <span class="nv">$timeout</span> <span class="o">=</span> <span class="mi">20000</span><span class="p">;</span> <span class="c1">// 20 seconds</span>
  195. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setTimeout</span><span class="p">(</span><span class="nv">$timeout</span><span class="p">);</span>
  196. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span></code></pre></figure>
  197. <p>If you are still having a problem then you should enable debugging, before you make the request, and check the debug log. This contains a dump of information from PhantomJS which could help to track down why you are not getting a response.</p>
  198. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  199. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  200. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  201. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">debug</span><span class="p">(</span><span class="k">true</span><span class="p">);</span> <span class="c1">// Set debug flag</span>
  202. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  203. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  204. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span>
  205. <span class="k">echo</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getLog</span><span class="p">();</span> <span class="c1">// Output log</span></code></pre></figure>
  206. <p>You can also try running a test script through your PhantomJS executable, from the command line, to see if you get a valid response back. Save the following script somewhere:</p>
  207. <figure class="highlight"><pre><code class="language-javascript" data-lang="javascript"> <span class="c1">//test-script.js</span>
  208. <span class="kd">var</span> <span class="nx">page</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;webpage&#39;</span><span class="p">).</span><span class="nx">create</span><span class="p">(),</span>
  209. <span class="nx">url</span> <span class="o">=</span> <span class="s1">&#39;YOUR REQUEST URL&#39;</span><span class="p">,</span> <span class="c1">// Change this to the URL you want to request</span>
  210. <span class="nx">response</span><span class="p">;</span>
  211. <span class="nx">page</span><span class="p">.</span><span class="nx">onResourceReceived</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">r</span><span class="p">)</span> <span class="p">{</span>
  212. <span class="nx">response</span> <span class="o">=</span> <span class="nx">r</span><span class="p">;</span>
  213. <span class="p">};</span>
  214. <span class="nx">phantom</span><span class="p">.</span><span class="nx">onError</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">msg</span><span class="p">,</span> <span class="nx">trace</span><span class="p">)</span> <span class="p">{</span>
  215. <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">msg</span><span class="p">);</span>
  216. <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">trace</span><span class="p">);</span>
  217. <span class="nx">phantom</span><span class="p">.</span><span class="nx">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
  218. <span class="p">};</span>
  219. <span class="nx">page</span><span class="p">.</span><span class="nx">open</span> <span class="p">(</span><span class="nx">url</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">status</span><span class="p">)</span> <span class="p">{</span>
  220. <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">status</span><span class="p">);</span>
  221. <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">response</span><span class="p">));</span>
  222. <span class="nx">phantom</span><span class="p">.</span><span class="nx">exit</span><span class="p">();</span>
  223. <span class="p">});</span></code></pre></figure>
  224. <p>And then, assuming you have saved the script above to <code>test-script.js</code> in the root of your project and your PhantomJS executable is located at <code>bin/phantomjs</code>, run the following:</p>
  225. <figure class="highlight"><pre><code class="language-bash" data-lang="bash"> <span class="c">#bash</span>
  226. <span class="nv">$ </span>bin/phantomjs ./test-script.js</code></pre></figure>
  227. <p>You should see an output of the response from PhantomJS:</p>
  228. <figure class="highlight"><pre><code class="language-bash" data-lang="bash"> <span class="c">#bash</span>
  229. success
  230. <span class="o">{</span><span class="s2">&quot;contentType&quot;</span>:<span class="s2">&quot;text/javascript; charset=UTF-8&quot;</span>, <span class="s2">&quot;headers&quot;</span>: ...</code></pre></figure>
  231. <p>If you don’t see <code>success</code> followed by a JSON encoded response object then there is something the with the URL you are requesting or your PhantomJS executable. Try reinstalling PhantomJS. If you see <code>fail</code> instead of <code>success</code>, chances are the URL you are requesting is invalid or not resolving.</p>
  232. <h4 id="its-not-saving-my-screenshots">It’s not saving my screenshots</h4>
  233. <p>When making a capture request you need to make sure that you are setting the file location that you want the screenshot saved to, including the filename:</p>
  234. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  235. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  236. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  237. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createCaptureRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  238. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  239. <span class="nv">$file</span> <span class="o">=</span> <span class="s1">&#39;/path/to/save/your/screen/capture/file.jpg&#39;</span><span class="p">;</span>
  240. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setCaptureFile</span><span class="p">(</span><span class="nv">$file</span><span class="p">);</span> <span class="c1">// Set the capture file</span>
  241. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span></code></pre></figure>
  242. <p>The file itself does not need to exist but the parent directory must exist and be writable by your application. Check that your application has permissions to write files to the directory you are setting for your screen captures.</p>
  243. <h4 id="can-i-set-the-screenshot-size">Can I set the screenshot size?</h4>
  244. <p>Yes, you can set the width and height of your capture along with the x and y coordinates of where the capture should start from:</p>
  245. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  246. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  247. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  248. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createCaptureRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  249. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  250. <span class="nv">$file</span> <span class="o">=</span> <span class="s1">&#39;/path/to/save/your/screen/capture/file.jpg&#39;</span><span class="p">;</span>
  251. <span class="nv">$top</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
  252. <span class="nv">$left</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
  253. <span class="nv">$width</span> <span class="o">=</span> <span class="mi">200</span><span class="p">;</span>
  254. <span class="nv">$height</span> <span class="o">=</span> <span class="mi">400</span><span class="p">;</span>
  255. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setCaptureFile</span><span class="p">(</span><span class="nv">$file</span><span class="p">);</span>
  256. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setCaptureDimensions</span><span class="p">(</span><span class="nv">$width</span><span class="p">,</span> <span class="nv">$height</span><span class="p">,</span> <span class="nv">$top</span><span class="p">,</span> <span class="nv">$left</span><span class="p">);</span>
  257. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span></code></pre></figure>
  258. <h4 id="can-i-set-the-viewport-size">Can I set the viewport size?</h4>
  259. <p>Yes, you can set the viewport dimensions on both regular and capture requests:</p>
  260. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  261. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  262. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  263. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  264. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  265. <span class="nv">$width</span> <span class="o">=</span> <span class="mi">200</span><span class="p">;</span>
  266. <span class="nv">$height</span> <span class="o">=</span> <span class="mi">400</span><span class="p">;</span>
  267. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setViewportSize</span><span class="p">(</span><span class="nv">$width</span><span class="p">,</span> <span class="nv">$height</span><span class="p">);</span> <span class="c1">// Set viewport size</span>
  268. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span></code></pre></figure>
  269. <h4 id="how-do-i-debug-a-request">How do I debug a request?</h4>
  270. <p>By setting the debug flag to <code>true</code> on the client, you can get a dump of information output from PhantomJS along with some info events added by the PHP PhantomJS library:</p>
  271. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  272. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  273. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  274. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">debug</span><span class="p">(</span><span class="k">true</span><span class="p">);</span> <span class="c1">// Set debug flag</span>
  275. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  276. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  277. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span>
  278. <span class="k">echo</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getLog</span><span class="p">();</span> <span class="c1">// Output log</span></code></pre></figure>
  279. <p>You can also get any javacript console errors along with a stack trace from the URL you are calling, in the response object:</p>
  280. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  281. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  282. <span class="nv">$client</span> <span class="o">=</span> <span class="nx">Client</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
  283. <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createRequest</span><span class="p">(</span><span class="s1">&#39;http://google.com&#39;</span><span class="p">);</span>
  284. <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">createResponse</span><span class="p">();</span>
  285. <span class="nv">$client</span><span class="o">-&gt;</span><span class="na">send</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span>
  286. <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getConsole</span><span class="p">());</span> <span class="c1">// Outputs array of console errors and stack trace</span></code></pre></figure>
  287. <h4 id="i-am-getting-syntaxerror-parse-error-in-the-debug-log">I am getting <code>SyntaxError: Parse error</code> in the debug log</h4>
  288. <p>You will only get this error if the script file that is being run by PhantomJS has a syntax error. If you are writing your own <a href="http://jonnnnyw.github.io/php-phantomjs/advanced.html#custom-phantomjs-scripts">custom scripts</a> then try setting the <a href="#how-do-i-debug-a-request">debug flag</a> which <em>should</em> print some more detailed information in the debug log. Also check that you aren’t setting any parameters to <code>null</code> in your request object as this could be causing a javascript error due to javascript variables being set to nothing e.g. <code>var width = ,</code>.</p>
  289. </div>
  290. </div>
  291. <div class="clearfix"></div>
  292. <hr>
  293. <ul class="pager">
  294. <li class="next"><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/4-advanced/" title="Advanced Usage">Advanced Usage &rarr;</a></li>
  295. </ul>
  296. </article>
  297. <footer>
  298. <hr/>
  299. <p>
  300. &copy; 2016 PHP PhantomJs
  301. </p>
  302. </footer>
  303. </div>
  304. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/jquery/jquery.min.js"></script>
  305. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/js/bootstrap.min.js"></script>
  306. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/js/app.js"></script>
  307. </body>
  308. </html>