index.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>PHP PhantomJS</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>4.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/4.0/">Introduction</a></li>
  68. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/">Installation</a></li>
  69. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/">Usage</a></li>
  70. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/">Custom Scripts</a></li>
  71. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/5-caching/">Caching</a></li>
  72. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/6-debugging/">Debugging</a></li>
  73. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/7-troubleshooting/">Troubleshooting</a></li>
  74. </ul>
  75. </div><!-- /.navbar-collapse -->
  76. </div>
  77. </nav>
  78. <!-- version-menu-dropdown -->
  79. <div class="btn-group hidden-xs" id="nav-version">
  80. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  81. <i class="fa fa-terminal"><b>4.0</b> version</i>
  82. </button>
  83. <ul class="dropdown-menu" role="menu">
  84. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/" title="4.0 version" class="active"><b>4.0</b> version</a></li>
  85. <li><a href="http://jonnnnyw.github.io/php-phantomjs/3.0/" title="3.0 version"><b>3.0</b> version</a></li>
  86. <li class="divider"></li>
  87. </ul>
  88. </div>
  89. <!-- nav-menu-dropdown -->
  90. <div class="btn-group hidden-xs" id="nav-menu">
  91. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  92. <i class="fa fa-bars"></i>
  93. </button>
  94. <ul class="dropdown-menu" role="menu">
  95. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/"><i class="fa fa-folder"></i>Introduction</a></li>
  96. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/"><i class="fa fa-folder"></i>Installation</a></li>
  97. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/"><i class="fa fa-folder"></i>Usage</a></li>
  98. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/"><i class="fa fa-folder"></i>Custom Scripts</a></li>
  99. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/5-caching/"><i class="fa fa-folder"></i>Caching</a></li>
  100. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/6-debugging/"><i class="fa fa-folder"></i>Debugging</a></li>
  101. <li><a href="http://jonnnnyw.github.io/php-phantomjs/4.0/7-troubleshooting/"><i class="fa fa-folder"></i>Troubleshooting</a></li>
  102. <li class="divider"></li>
  103. <li><a href="#"><i class="fa fa-arrow-up"></i>Top of Page</a></li>
  104. </ul>
  105. </div>
  106. <div class="col-sm-3 sidebar hidden-xs">
  107. <! -- sidebar.html -->
  108. <header class="sidebar-header" role="banner">
  109. <a href="http://jonnnnyw.github.io/php-phantomjs/">
  110. <img src="http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs.png" class="img-circle" />
  111. </a>
  112. <h3 class="title">
  113. <a href="http://jonnnnyw.github.io/php-phantomjs/">PHP PhantomJs</a>
  114. </h3>
  115. </header>
  116. <div id="bio" class="text-center">
  117. Run PhantomJS scripts through PHP
  118. </div>
  119. <div id="contact-list" class="text-center">
  120. <ul class="list-unstyled list-inline">
  121. <li>
  122. <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=watch&count=true"
  123. allowtransparency="true" frameborder="0" scrolling="0" width="85" height="20"></iframe>
  124. </li>
  125. <li>
  126. <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=fork&count=true"
  127. allowtransparency="true" frameborder="0" scrolling="0" width="83" height="20"></iframe>
  128. </li>
  129. </ul>
  130. </div>
  131. <! -- sidebar.html end -->
  132. </div>
  133. <p id="version-notice">You are currently viewing version <span>4.0</span> documentation.</p>
  134. <div class="col-sm-9 col-sm-offset-3">
  135. <div class="page-header">
  136. <h1>Introduction </h1>
  137. </div>
  138. <article class="home">
  139. <div>
  140. <p>PHP PhantomJS is a flexible PHP library to load pages through the PhantomJS headless browser and return the page response. It is handy for testing websites that demand javascript support and also supports screen captures and PDF output.</p>
  141. <h2 id="feature-list">Feature List</h2>
  142. <ul>
  143. <li>Load webpages through the PhantomJS headless browser</li>
  144. <li>View detailed response data including page content, headers, status
  145. code etc.</li>
  146. <li>Handle redirects</li>
  147. <li>View javascript console errors</li>
  148. <li>View detailed PhantomJS debuged information</li>
  149. <li>Save screen captures to local disk</li>
  150. <li>Output web pages to PDF document</li>
  151. <li>Set viewport size</li>
  152. <li>Set fixed header and footer for PDF output</li>
  153. <li>Define screen capture x, y, width and height parameters</li>
  154. <li>Delay page rendering for a specified time</li>
  155. <li>Delay page rendering until page resources are fully loaded</li>
  156. <li>Execute PhantomJS with command line options</li>
  157. <li>Easily build and run custom PhantomJS scripts</li>
  158. </ul>
  159. <h2 id="prerequisites">Prerequisites</h2>
  160. <p>PHP PhantomJS requires PHP <strong>5.3.0</strong> or greater to run.</p>
  161. <h2 id="installation">Installation</h2>
  162. <p>It is recommended that you use Composer to install PHP PhantomJS. First, add the following to your project’s <code>composer.json</code> file:</p>
  163. <figure class="highlight"><pre><code class="language-yaml" data-lang="yaml">
  164. <span class="c1">#composer.json</span>
  165. <span class="s">&quot;scripts&quot;</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span>
  166. <span class="s">&quot;post-install-cmd&quot;</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span>
  167. <span class="s">&quot;PhantomInstaller\\Installer::installPhantomJS&quot;</span>
  168. <span class="p-Indicator">],</span>
  169. <span class="s">&quot;post-update-cmd&quot;</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span>
  170. <span class="s">&quot;PhantomInstaller\\Installer::installPhantomJS&quot;</span>
  171. <span class="p-Indicator">]</span>
  172. <span class="p-Indicator">}</span>
  173. </code></pre></figure>
  174. <p>This will ensure the latest version of PhantomJS is installed for your system, in your bin folder. If you haven’t defined your bin folder in your composer.json, add the path:</p>
  175. <figure class="highlight"><pre><code class="language-yaml" data-lang="yaml">
  176. <span class="c1">#composer.json</span>
  177. <span class="s">&quot;config&quot;</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span>
  178. <span class="s">&quot;bin-dir&quot;</span><span class="p-Indicator">:</span> <span class="s">&quot;bin&quot;</span>
  179. <span class="p-Indicator">}</span></code></pre></figure>
  180. <p>Finally, install PHP PhantomJS from the root of your project:</p>
  181. <figure class="highlight"><pre><code class="language-bash" data-lang="bash">
  182. <span class="c">#bash</span>
  183. <span class="nv">$ </span>composer require <span class="s2">&quot;jonnyw/php-phantomjs:4.*&quot;</span></code></pre></figure>
  184. <p>If you would like to use another installation method or would like to see more detailed installation instructions, see the <a href="http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/">installation</a> documentation.</p>
  185. <blockquote>
  186. <h4 id="important">Important</h4>
  187. <p>By default the PhantomJS library will look for the PhantomJS executable in the bin folder relative to where your script is running <code>~/bin/phantomjs</code>. If the executable cannot be found or if the path to your PhantomJS executable differs from the default location, for example you have installed PhantomJS globally, you will need to define the path to your PhantomJS executable manually.</p>
  188. <p><code>$client-&gt;getEngine()-&gt;setPath('/path/to/phantomjs');</code></p>
  189. </blockquote>
  190. <h2 id="basic-usage">Basic Usage</h2>
  191. <p>The following illustrates how to make a basic GET request and output the page content:</p>
  192. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  193. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  194. <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>
  195. <span class="sd">/** </span>
  196. <span class="sd"> * @see JonnyW\PhantomJs\Http\Request</span>
  197. <span class="sd"> **/</span>
  198. <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://jonnyw.me&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">);</span>
  199. <span class="sd">/** </span>
  200. <span class="sd"> * @see JonnyW\PhantomJs\Http\Response </span>
  201. <span class="sd"> **/</span>
  202. <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>
  203. <span class="c1">// Send the request</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">if</span><span class="p">(</span><span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getStatus</span><span class="p">()</span> <span class="o">===</span> <span class="mi">200</span><span class="p">)</span> <span class="p">{</span>
  206. <span class="c1">// Dump the requested page content</span>
  207. <span class="k">echo</span> <span class="nv">$response</span><span class="o">-&gt;</span><span class="na">getContent</span><span class="p">();</span>
  208. <span class="p">}</span>
  209. </code></pre></figure>
  210. <p>Saving a screen capture to local disk:</p>
  211. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  212. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  213. <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>
  214. <span class="nv">$width</span> <span class="o">=</span> <span class="mi">800</span><span class="p">;</span>
  215. <span class="nv">$height</span> <span class="o">=</span> <span class="mi">600</span><span class="p">;</span>
  216. <span class="nv">$top</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  217. <span class="nv">$left</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  218. <span class="sd">/** </span>
  219. <span class="sd"> * @see JonnyW\PhantomJs\Http\CaptureRequest</span>
  220. <span class="sd"> **/</span>
  221. <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://jonnyw.me&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">);</span>
  222. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setOutputFile</span><span class="p">(</span><span class="s1">&#39;/path/to/save/capture/file.jpg&#39;</span><span class="p">);</span>
  223. <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>
  224. <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>
  225. <span class="sd">/** </span>
  226. <span class="sd"> * @see JonnyW\PhantomJs\Http\Response </span>
  227. <span class="sd"> **/</span>
  228. <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>
  229. <span class="c1">// Send the request</span>
  230. <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>
  231. </code></pre></figure>
  232. <p>Outputting a page as PDF:</p>
  233. <figure class="highlight"><pre><code class="language-php" data-lang="php"><span class="x"> </span><span class="cp">&lt;?php</span>
  234. <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
  235. <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>
  236. <span class="sd">/** </span>
  237. <span class="sd"> * @see JonnyW\PhantomJs\Http\PdfRequest</span>
  238. <span class="sd"> **/</span>
  239. <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">createPdfRequest</span><span class="p">(</span><span class="s1">&#39;http://jonnyw.me&#39;</span><span class="p">,</span> <span class="s1">&#39;GET&#39;</span><span class="p">);</span>
  240. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setOutputFile</span><span class="p">(</span><span class="s1">&#39;/path/to/save/pdf/document.pdf&#39;</span><span class="p">);</span>
  241. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setFormat</span><span class="p">(</span><span class="s1">&#39;A4&#39;</span><span class="p">);</span>
  242. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setOrientation</span><span class="p">(</span><span class="s1">&#39;landscape&#39;</span><span class="p">);</span>
  243. <span class="nv">$request</span><span class="o">-&gt;</span><span class="na">setMargin</span><span class="p">(</span><span class="s1">&#39;1cm&#39;</span><span class="p">);</span>
  244. <span class="sd">/** </span>
  245. <span class="sd"> * @see JonnyW\PhantomJs\Http\Response </span>
  246. <span class="sd"> **/</span>
  247. <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>
  248. <span class="c1">// Send the request</span>
  249. <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>
  250. </code></pre></figure>
  251. <p>For more detailed examples see the <a href="http://jonnnnyw.github.io/php-phantomjs/4.0/3-usage/">usage</a> section, or you can <a href="http://jonnnnyw.github.io/php-phantomjs/4.0/4-custom-scripts/">create your own custom scripts</a>.</p>
  252. </div>
  253. <div class="clearfix"></div>
  254. <hr/>
  255. <ul class="pager">
  256. <li class="next">
  257. <a href="http://jonnnnyw.github.io/php-phantomjs/4.0/2-installation/" title="Installation">Installation &rarr;</a>
  258. </li>
  259. </ul>
  260. </article>
  261. <footer>
  262. <hr/>
  263. <p>
  264. &copy; 2016 PHP PhantomJs
  265. </p>
  266. </footer>
  267. </div>
  268. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/jquery/jquery.min.js"></script>
  269. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/js/bootstrap.min.js"></script>
  270. <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/js/app.js"></script>
  271. </body>
  272. </html>