|
|
@@ -15,10 +15,10 @@
|
|
|
<![endif]-->
|
|
|
|
|
|
<!-- Le styles -->
|
|
|
- <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
|
|
|
- <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
|
|
|
- <link href="http://jonnnnyw.github.io/php-phantomjs/assets/resources/syntax/syntax.css" rel="stylesheet">
|
|
|
- <link href="http://jonnnnyw.github.io/php-phantomjs/assets/css/style.css" rel="stylesheet">
|
|
|
+ <link href="/assets/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
|
|
|
+ <link href="/assets/resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
|
|
|
+ <link href="/assets/resources/syntax/syntax.css" rel="stylesheet">
|
|
|
+ <link href="/assets/css/style.css" rel="stylesheet">
|
|
|
|
|
|
<!-- Le fav and touch icons -->
|
|
|
<!-- Update these with your own images
|
|
|
@@ -28,8 +28,8 @@
|
|
|
<link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
|
|
|
-->
|
|
|
|
|
|
- <link rel="alternate" type="application/rss+xml" title="" href="http://jonnnnyw.github.io/php-phantomjs/feed.xml">
|
|
|
-
|
|
|
+ <link rel="alternate" type="application/rss+xml" title="" href="/feed.xml">
|
|
|
+
|
|
|
<script>
|
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
|
@@ -39,7 +39,7 @@
|
|
|
ga('create', 'UA-53289013-1', 'auto');
|
|
|
ga('send', 'pageview');
|
|
|
</script>
|
|
|
-
|
|
|
+
|
|
|
</head>
|
|
|
|
|
|
<body>
|
|
|
@@ -53,15 +53,18 @@
|
|
|
<span class="icon-bar"></span>
|
|
|
</button>
|
|
|
|
|
|
- <!--
|
|
|
|
|
|
+ <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=fork&count=true"
|
|
|
+ allowtransparency="true" frameborder="0" scrolling="0" width="85" height="20" class="nav-link last"></iframe>
|
|
|
+
|
|
|
|
|
|
|
|
|
-
|
|
|
- -->
|
|
|
-
|
|
|
- <a class="navbar-brand" href="http://jonnnnyw.github.io/php-phantomjs/">
|
|
|
- <img src="http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs-small.png" class="img-circle" />
|
|
|
+ <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=watch&count=true"
|
|
|
+ allowtransparency="true" frameborder="0" scrolling="0" width="85" height="20" class="nav-link"></iframe>
|
|
|
+
|
|
|
+
|
|
|
+ <a class="navbar-brand" href="/">
|
|
|
+ <img src="/assets/media/phantomjs-small.png" class="img-circle" />
|
|
|
PHP PhantomJs
|
|
|
</a>
|
|
|
</div>
|
|
|
@@ -69,12 +72,11 @@
|
|
|
<!-- Collect the nav links, forms, and other content for toggling -->
|
|
|
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
|
|
<ul class="nav navbar-nav">
|
|
|
- <li class="active"><a href="http://jonnnnyw.github.io/php-phantomjs/">Home</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/installation.html">Installation</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/usage.html">Usage</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/advanced.html">Advanced</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/debugging.html">Debugging</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/troubleshooting.html">Troubleshooting</a></li>
|
|
|
+ <li class="active"><a href="/">Home</a></li>
|
|
|
+ <li><a href="/installation.html">Installation</a></li>
|
|
|
+ <li><a href="/usage.html">Usage</a></li>
|
|
|
+ <li><a href="/advanced.html">Advanced</a></li>
|
|
|
+ <li><a href="/troubleshooting.html">Troubleshooting</a></li>
|
|
|
</ul>
|
|
|
</div><!-- /.navbar-collapse -->
|
|
|
</nav>
|
|
|
@@ -85,12 +87,11 @@
|
|
|
<i class="fa fa-bars"></i>
|
|
|
</button>
|
|
|
<ul class="dropdown-menu" role="menu">
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/"><i class="fa fa-home"></i>Home</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/installation.html"><i class="fa fa-folder"></i>Installation</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/usage.html"><i class="fa fa-folder"></i>Usage</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/advanced.html"><i class="fa fa-folder"></i>Advanced</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/debugging.html"><i class="fa fa-folder"></i>Debugging</a></li>
|
|
|
- <li><a href="http://jonnnnyw.github.io/php-phantomjs/troubleshooting.html"><i class="fa fa-folder"></i>Troubleshooting</a></li>
|
|
|
+ <li><a href="/"><i class="fa fa-home"></i>Home</a></li>
|
|
|
+ <li><a href="/installation.html"><i class="fa fa-folder"></i>Installation</a></li>
|
|
|
+ <li><a href="/usage.html"><i class="fa fa-folder"></i>Usage</a></li>
|
|
|
+ <li><a href="/advanced.html"><i class="fa fa-folder"></i>Advanced</a></li>
|
|
|
+ <li><a href="/troubleshooting.html"><i class="fa fa-folder"></i>Troubleshooting</a></li>
|
|
|
<li class="divider"></li>
|
|
|
<li><a href="#"><i class="fa fa-arrow-up"></i>Top of Page</a></li>
|
|
|
</ul>
|
|
|
@@ -99,11 +100,11 @@
|
|
|
<div class="col-sm-3 sidebar hidden-xs">
|
|
|
<! -- sidebar.html -->
|
|
|
<header class="sidebar-header" role="banner">
|
|
|
- <a href="http://jonnnnyw.github.io/php-phantomjs/">
|
|
|
- <img src="http://jonnnnyw.github.io/php-phantomjs/assets/media/phantomjs.png" class="img-circle" />
|
|
|
+ <a href="/">
|
|
|
+ <img src="/assets/media/phantomjs.png" class="img-circle" />
|
|
|
</a>
|
|
|
<h3 class="title">
|
|
|
- <a href="http://jonnnnyw.github.io/php-phantomjs/">PHP PhantomJs</a>
|
|
|
+ <a href="/">PHP PhantomJs</a>
|
|
|
</h3>
|
|
|
</header>
|
|
|
|
|
|
@@ -114,22 +115,22 @@
|
|
|
|
|
|
|
|
|
<div id="contact-list" class="text-center">
|
|
|
-<!--
|
|
|
+
|
|
|
<ul class="list-unstyled list-inline">
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- </ul>
|
|
|
- <ul id="contact-list-secondary" class="list-unstyled list-inline">
|
|
|
+ <li>
|
|
|
+ <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=watch&count=true"
|
|
|
+ allowtransparency="true" frameborder="0" scrolling="0" width="83" height="20"></iframe>
|
|
|
+ </li>
|
|
|
|
|
|
|
|
|
<li>
|
|
|
- <a class="btn btn-default btn-sm" href="http://jonnnnyw.github.io/php-phantomjs/feed.xml">
|
|
|
- <i class="fa fa-rss fa-lg"></i>
|
|
|
- </a>
|
|
|
+ <iframe src="http://ghbtns.com/github-btn.html?user=jonnnnyw&repo=php-phantomjs&type=fork&count=true"
|
|
|
+ allowtransparency="true" frameborder="0" scrolling="0" width="83" height="20"></iframe>
|
|
|
</li>
|
|
|
+
|
|
|
</ul>
|
|
|
--->
|
|
|
+
|
|
|
</div>
|
|
|
<! -- sidebar.html end -->
|
|
|
|
|
|
@@ -140,355 +141,138 @@
|
|
|
|
|
|
|
|
|
<div class="page-header">
|
|
|
- <h1>Advanced Usage </h1>
|
|
|
+ <h1>Introduction </h1>
|
|
|
</div>
|
|
|
|
|
|
<article class="home">
|
|
|
|
|
|
<div>
|
|
|
|
|
|
- <ul>
|
|
|
- <li><a href="#phantomjs-command-line-options">PhantomJS command line options</a></li>
|
|
|
- <li><a href="#custom-phantom-js-scripts">Custom PhantomJS scripts</a>
|
|
|
- <ul>
|
|
|
- <li><a href="#writing-a-custom-script">Writing a custom script</a></li>
|
|
|
- <li><a href="#using-custom-request-parameters-in-your-script">Using custom request parameters in your script</a></li>
|
|
|
- <li><a href="#loading-your-script">Loading your script</a></li>
|
|
|
- </ul>
|
|
|
- </li>
|
|
|
+ <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.</p>
|
|
|
+
|
|
|
+<h2 id="feature-list">Feature List</h2>
|
|
|
+
|
|
|
+<ul>
|
|
|
+ <li>Load webpages through the PhantomJS headless browser</li>
|
|
|
+ <li>View detailed response data including page content, headers, status<br />
|
|
|
+code etc.</li>
|
|
|
+ <li>Handle redirects</li>
|
|
|
+ <li>View javascript console errors</li>
|
|
|
+ <li>View detailed PhantomJS debuged information</li>
|
|
|
+ <li>Save screen captures to local disk</li>
|
|
|
+ <li>Define screen capture x, y, width and height parameters</li>
|
|
|
+ <li>Delay page rendering for a specified time</li>
|
|
|
+ <li>Execute PhantomJS with command line options</li>
|
|
|
+ <li>Easily build and run custom PhantomJS scripts</li>
|
|
|
</ul>
|
|
|
|
|
|
-<h2 id="phantomjs-command-line-options">PhantomJS command line options</h2>
|
|
|
+<h2 id="prerequisites">Prerequisites</h2>
|
|
|
|
|
|
-<p>The PhantomJS API contains a range of command line options that can be passed when executing the PhantomJS executable. These can also be passed in via the client before a request:</p>
|
|
|
+<p>PHP PhantomJS requires PHP <strong>5.3.0</strong> or greater to run.</p>
|
|
|
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
+<h2 id="installation">Installation</h2>
|
|
|
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
-
|
|
|
- <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>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">addOption</span><span class="p">(</span><span class="s1">'--load-images=true'</span><span class="p">);</span>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">addOption</span><span class="p">(</span><span class="s1">'--ignore-ssl-errors=true'</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">(</span><span class="s1">'http://google.com'</span><span class="p">);</span>
|
|
|
- <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createResponse</span><span class="p">();</span>
|
|
|
+<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>
|
|
|
|
|
|
- <span class="nv">$client</span><span class="o">-></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></div>
|
|
|
+<div class="highlight"><pre><code class="language-yaml" data-lang="yaml"><span class="c1">#composer.json</span>
|
|
|
|
|
|
-<p>You can also set a path to a JSON configuration file that contains multiple PhantomJS options:</p>
|
|
|
+ <span class="s">"scripts"</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span>
|
|
|
+ <span class="s">"post-install-cmd"</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span>
|
|
|
+ <span class="s">"PhantomInstaller\\Installer::installPhantomJS"</span>
|
|
|
+ <span class="p-Indicator">],</span>
|
|
|
+ <span class="s">"post-update-cmd"</span><span class="p-Indicator">:</span> <span class="p-Indicator">[</span>
|
|
|
+ <span class="s">"PhantomInstaller\\Installer::installPhantomJS"</span>
|
|
|
+ <span class="p-Indicator">]</span>
|
|
|
+ <span class="p-Indicator">}</span></code></pre></div>
|
|
|
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
+<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>
|
|
|
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
-
|
|
|
- <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>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">addOption</span><span class="p">(</span><span class="s1">'--config=/path/to/config.json'</span><span class="p">);</span>
|
|
|
+<div class="highlight"><pre><code class="language-yaml" data-lang="yaml"><span class="c1">#composer.json</span>
|
|
|
|
|
|
- <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">(</span><span class="s1">'http://google.com'</span><span class="p">);</span>
|
|
|
- <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createResponse</span><span class="p">();</span>
|
|
|
+ <span class="s">"config"</span><span class="p-Indicator">:</span> <span class="p-Indicator">{</span>
|
|
|
+ <span class="s">"bin-dir"</span><span class="p-Indicator">:</span> <span class="s">"bin"</span>
|
|
|
+ <span class="p-Indicator">}</span></code></pre></div>
|
|
|
|
|
|
- <span class="nv">$client</span><span class="o">-></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></div>
|
|
|
+<p>Finally, install PHP PhantomJS from the root of your project:</p>
|
|
|
|
|
|
-<p>See the <a href="http://phantomjs.org/api/command-line.html">PhantomJS Documentation</a> for a full list of command line options.</p>
|
|
|
+<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#bash</span>
|
|
|
+
|
|
|
+ <span class="nv">$ </span>composer require <span class="s2">"jonnyw/php-phantomjs:3.*"</span></code></pre></div>
|
|
|
|
|
|
-<h2 id="custom-phantomjs-scripts">Custom PhantomJS scripts</h2>
|
|
|
+<p>If you would like to use another installation method or would like to see more detailed installation instructions, see the <a href="/installation.html">installation</a> documentation.</p>
|
|
|
|
|
|
-<p>In most instances you shouldn’t need to worry about the javascript files that run the PHP PhantomJS library but there may be times when you want to execute your own custom PhantomJS scripts through the client. This can be easily achieved by using the built in script loader.</p>
|
|
|
+<h2 id="basic-usage">Basic Usage</h2>
|
|
|
|
|
|
-<p>Script files or ‘procedures’ as they are referred to in the application are closely mapped to requests. When you create a default request instance, you are essentially running the default javascript procedure that comes bundled with the application. When you create a capture request you are running the capture procedure.</p>
|
|
|
+<p>The following illustrates how to make a basic GET request and output the page content:</p>
|
|
|
|
|
|
<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
|
|
|
<span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">();</span> <span class="c1">// ~/Resources/procedures/default.proc</span>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createCaptureRequest</span><span class="p">();</span> <span class="c1">// ~/Resources/procedures/capture.proc</span></code></pre></div>
|
|
|
-
|
|
|
-<h3 id="writing-a-custom-script">Writing a custom script</h3>
|
|
|
-
|
|
|
-<p>The first step in creating your script is to create a procedure file somewhere. For the purpose of this guide we will refer to it as <code>my_procedure.proc</code> but in reality it can be called anything you like. The only requirement is that the file extension must be <code>.proc</code>.</p>
|
|
|
-
|
|
|
-<p>Create the file somewhere and make sure it can be read by your application. Make a note of the path to the directory where your file is created as you will need this when loading your script which is explained later in this guide.</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-bash" data-lang="bash"><span class="c">#bash</span>
|
|
|
-
|
|
|
- <span class="nv">$ </span>touch my_procedure.proc
|
|
|
- <span class="nv">$ </span>chmod <span class="m">755</span> my_procedure.proc</code></pre></div>
|
|
|
-
|
|
|
-<p>Next open your procedure file in your text editor and write your PhantomJS script. The <a href="http://phantomjs.org/quick-start.html">PhantomJS documentation</a> has more detailed information on writing custom scripts.</p>
|
|
|
|
|
|
-<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// my_procedure.proc</span>
|
|
|
-
|
|
|
- <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">'webpage'</span><span class="p">).</span><span class="nx">create</span><span class="p">();</span>
|
|
|
-
|
|
|
- <span class="nx">page</span><span class="p">.</span><span class="nx">open</span> <span class="p">(</span><span class="s1">'{{ request.getUrl() }}'</span><span class="p">,</span> <span class="s1">'{{ request.getMethod() }}'</span><span class="p">,</span> <span class="s1">'{{ request.getBody() }}'</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>
|
|
|
-
|
|
|
- <span class="c1">// It is important that you exit PhantomJS</span>
|
|
|
- <span class="c1">// when your script has run or when you</span>
|
|
|
- <span class="c1">// encounter an error</span>
|
|
|
- <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>
|
|
|
- <span class="p">});</span>
|
|
|
-
|
|
|
- <span class="p">...</span></code></pre></div>
|
|
|
-
|
|
|
-<blockquote>
|
|
|
- <h4 id="important">Important</h4>
|
|
|
- <p>Make sure that <code>phantom.exit(1);</code> is always called after your script has run or if you encounter an error. This requires you to take care when handling PhantomJS errors to ensure that you exit the PhantomJS script, whether the script was successfully executed or not. If you do not call <code>phantom.exit(1);</code> then PhantomJS will continue to run until your PHP script times out. If you find that your custom script is hanging then this is most likely the cause.</p>
|
|
|
-</blockquote>
|
|
|
+ <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>
|
|
|
|
|
|
-<p>It is a good practice to create a global error handler in your script that exits PhantomJS:</p>
|
|
|
+ <span class="sd">/** </span>
|
|
|
+<span class="sd"> * @see JonnyW\PhantomJs\Message\Request </span>
|
|
|
+<span class="sd"> **/</span>
|
|
|
+ <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">(</span><span class="s1">'http://google.com'</span><span class="p">,</span> <span class="s1">'GET'</span><span class="p">);</span>
|
|
|
|
|
|
-<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// my_procedure.proc</span>
|
|
|
+ <span class="sd">/** </span>
|
|
|
+<span class="sd"> * @see JonnyW\PhantomJs\Message\Response </span>
|
|
|
+<span class="sd"> **/</span>
|
|
|
+ <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createResponse</span><span class="p">();</span>
|
|
|
|
|
|
- <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>
|
|
|
-
|
|
|
- <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>
|
|
|
- <span class="p">};</span>
|
|
|
-
|
|
|
- <span class="p">...</span></code></pre></div>
|
|
|
-
|
|
|
-<h3 id="using-custom-request-parameters-in-your-script">Using custom request parameters in your script</h3>
|
|
|
-
|
|
|
-<p>Before a procedure is executed by the application it is parsed through a template parser. The PHP PhantomJS library uses the popular <a href="https://github.com/fabpot/Twig">Twig templating engine</a>. This gives you access to all the <a href="http://twig.sensiolabs.org/doc/templates.html">Twig niceness</a> which you can use in your custom scripts.</p>
|
|
|
-
|
|
|
-<p>You may have noticed in the example above that we have used some Twig template tags referencing a request object e.g. <code>{{ request.getUrl() }}</code>. This is in fact the PHP request instance that you created and passed to the client when sending your request, which is injected into the Twig template parser. As a result you gain full access to all the data contained within the request instance, via the data accessor methods.</p>
|
|
|
-
|
|
|
-<p>A default request instance contains the following accessors:</p>
|
|
|
-
|
|
|
-<table>
|
|
|
- <thead>
|
|
|
- <tr>
|
|
|
- <th style="text-align: center">Accessor</th>
|
|
|
- <th>Description</th>
|
|
|
- <th style="text-align: center">Twig example</th>
|
|
|
- </tr>
|
|
|
- </thead>
|
|
|
- <tbody>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getMethod()</td>
|
|
|
- <td>The request method e.g. GET.</td>
|
|
|
- <td style="text-align: center">{{ request.getMethod() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getTimeout()</td>
|
|
|
- <td>The request timeout period in milliseconds.</td>
|
|
|
- <td style="text-align: center">{{ request.getTimeout() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getDelay()</td>
|
|
|
- <td>The page render delay in seconds.</td>
|
|
|
- <td style="text-align: center">{{ request.getDelay() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getUrl()</td>
|
|
|
- <td>The request URL.</td>
|
|
|
- <td style="text-align: center">{{ request.getUrl() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getBody()</td>
|
|
|
- <td>The request body (POST, PUT).</td>
|
|
|
- <td style="text-align: center">{{ request.getBody() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getHeaders(<em>format</em>)</td>
|
|
|
- <td>The request headers.</td>
|
|
|
- <td style="text-align: center">{{ request.getHeaders(‘json’) }}</td>
|
|
|
- </tr>
|
|
|
- </tbody>
|
|
|
-</table>
|
|
|
-
|
|
|
-<p>A capture request contains a few additional ones:</p>
|
|
|
-
|
|
|
-<table>
|
|
|
- <thead>
|
|
|
- <tr>
|
|
|
- <th style="text-align: center">Accessor</th>
|
|
|
- <th>Description</th>
|
|
|
- <th style="text-align: center">Twig example</th>
|
|
|
- </tr>
|
|
|
- </thead>
|
|
|
- <tbody>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getRectTop()</td>
|
|
|
- <td>The x coordinate of the capture region.</td>
|
|
|
- <td style="text-align: center">{{ request.getRectTop() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getRectLeft()</td>
|
|
|
- <td>The y coordinate of the capture region.</td>
|
|
|
- <td style="text-align: center">{{ request.getRectLeft() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getRectWidth()</td>
|
|
|
- <td>The width of the capture region.</td>
|
|
|
- <td style="text-align: center">{{ request.getRectWidth() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getRectHeight()</td>
|
|
|
- <td>The height of the capture region.</td>
|
|
|
- <td style="text-align: center">{{ request.getRectHeight() }}</td>
|
|
|
- </tr>
|
|
|
- <tr>
|
|
|
- <td style="text-align: center">getCaptureFile()</td>
|
|
|
- <td>The file to save the capture to.</td>
|
|
|
- <td style="text-align: center">{{ request.getCaptureFile() }}</td>
|
|
|
- </tr>
|
|
|
- </tbody>
|
|
|
-</table>
|
|
|
-
|
|
|
-<p>If you would like to inject additional data into your script through custom accessors, simply extend the request class with your own:</p>
|
|
|
+ <span class="c1">// Send the request</span>
|
|
|
+ <span class="nv">$client</span><span class="o">-></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>
|
|
|
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
+ <span class="k">if</span><span class="p">(</span><span class="nv">$response</span><span class="o">-></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>
|
|
|
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Message\Request</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="k">class</span> <span class="nc">CustomRequest</span> <span class="k">extends</span> <span class="nx">Request</span>
|
|
|
- <span class="p">{</span>
|
|
|
-
|
|
|
- <span class="k">public</span> <span class="k">function</span> <span class="nf">getSomething</span><span class="p">()</span>
|
|
|
- <span class="p">{</span>
|
|
|
- <span class="k">return</span> <span class="s1">'Something!'</span><span class="p">;</span>
|
|
|
- <span class="p">}</span>
|
|
|
+ <span class="c1">// Dump the requested page content</span>
|
|
|
+ <span class="k">echo</span> <span class="nv">$response</span><span class="o">-></span><span class="na">getContent</span><span class="p">();</span>
|
|
|
<span class="p">}</span></code></pre></div>
|
|
|
|
|
|
-<p>Now you will be able to access the data in your custom script when using your custom request:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// my_procedure.proc</span>
|
|
|
-
|
|
|
- <span class="kd">var</span> <span class="nx">something</span> <span class="o">=</span> <span class="s1">'{{ request.getSomething() }}'</span><span class="p">;</span> <span class="c1">// Get something</span>
|
|
|
-
|
|
|
- <span class="p">...</span></code></pre></div>
|
|
|
-
|
|
|
-<p>And to use your custom request simply create a new instance of it and pass it to the client:</p>
|
|
|
+<p>And if you would like to save a screen capture to local disk:</p>
|
|
|
|
|
|
<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
|
|
|
<span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
-
|
|
|
- <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>
|
|
|
-
|
|
|
- <span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createResponse</span><span class="p">();</span>
|
|
|
-
|
|
|
- <span class="nv">$request</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">CustomRequest</span><span class="p">();</span>
|
|
|
- <span class="nv">$request</span><span class="o">-></span><span class="na">setMethod</span><span class="p">(</span><span class="s1">'GET'</span><span class="p">);</span>
|
|
|
- <span class="nv">$request</span><span class="o">-></span><span class="na">setUrl</span><span class="p">(</span><span class="s1">'http://www.google.com'</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="nv">$client</span><span class="o">-></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></div>
|
|
|
-
|
|
|
-<h3 id="loading-your-script">Loading your script</h3>
|
|
|
-
|
|
|
-<p>Now that you have your custom script and you’ve added your custom request parameters, you may be wondering how to tell the client to actually load your script. This is done by creating a procedure loader and telling it where to find your script files.</p>
|
|
|
-
|
|
|
-<p>The service container has a factory that makes creating a new procedure loader easy:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
-
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\DependencyInjection\ServiceContainer</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="nv">$location</span> <span class="o">=</span> <span class="s1">'/path/to/your/procedure/directory'</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="nv">$serviceContainer</span> <span class="o">=</span> <span class="nx">ServiceContainer</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
|
|
|
-
|
|
|
- <span class="nv">$procedureLoader</span> <span class="o">=</span> <span class="nv">$serviceContainer</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'procedure_loader_factory'</span><span class="p">)</span>
|
|
|
- <span class="o">-></span><span class="na">createProcedureLoader</span><span class="p">(</span><span class="nv">$location</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="o">...</span></code></pre></div>
|
|
|
|
|
|
-<p>The client contains a chain procedure loader which lets you set multiple loaders at the same time. Ultimately this means that you can load your custom scripts while still maintaining the ability to load the default scripts if you choose. </p>
|
|
|
-
|
|
|
-<p>Now add your procedure loader to the chain loader:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
-
|
|
|
- <span class="o">...</span>
|
|
|
-
|
|
|
<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>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">getProcedureLoader</span><span class="p">()</span><span class="o">-></span><span class="na">addLoader</span><span class="p">(</span><span class="nv">$procedureLoader</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="o">...</span></code></pre></div>
|
|
|
-
|
|
|
-<p>The last thing you need to do is to tell the request which script you want to load for that request. This is done by setting the request type to the name of your procedure file, minus the extension:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
-
|
|
|
- <span class="o">...</span>
|
|
|
-
|
|
|
- <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">();</span>
|
|
|
- <span class="nv">$request</span><span class="o">-></span><span class="na">setType</span><span class="p">(</span><span class="s1">'my_procedure'</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="o">...</span></code></pre></div>
|
|
|
-
|
|
|
-<p>Or if you are using a custom request as outlined in the <a href="#using-custom-request-parameters-in-your-script">custom request parameters</a> section, you can implement a <code>getType()</code> method which returns the name of your procedure, eliminating the need to set the request type for each request:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Message\Request</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="k">class</span> <span class="nc">CustomRequest</span> <span class="k">extends</span> <span class="nx">Request</span>
|
|
|
- <span class="p">{</span>
|
|
|
-
|
|
|
- <span class="k">public</span> <span class="k">function</span> <span class="nf">getType</span><span class="p">()</span>
|
|
|
- <span class="p">{</span>
|
|
|
- <span class="k">return</span> <span class="s1">'my_procedure'</span><span class="p">;</span>
|
|
|
- <span class="p">}</span>
|
|
|
- <span class="p">}</span></code></pre></div>
|
|
|
+ <span class="sd">/** </span>
|
|
|
+<span class="sd"> * @see JonnyW\PhantomJs\Message\CaptureRequest</span>
|
|
|
+<span class="sd"> **/</span>
|
|
|
+ <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createCaptureRequest</span><span class="p">(</span><span class="s1">'http://google.com'</span><span class="p">,</span> <span class="s1">'GET'</span><span class="p">);</span>
|
|
|
+ <span class="nv">$request</span><span class="o">-></span><span class="na">setCaptureFile</span><span class="p">(</span><span class="s1">'/path/to/save/capture/file.jpg'</span><span class="p">);</span>
|
|
|
|
|
|
-<p>Below is a full example for clarity:</p>
|
|
|
-
|
|
|
-<div class="highlight"><pre><code class="language-php" data-lang="php"><span class="cp"><?php</span>
|
|
|
-
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\Client</span><span class="p">;</span>
|
|
|
- <span class="k">use</span> <span class="nx">JonnyW\PhantomJs\DependencyInjection\ServiceContainer</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="nv">$location</span> <span class="o">=</span> <span class="s1">'/path/to/your/procedure/directory'</span><span class="p">;</span>
|
|
|
-
|
|
|
- <span class="nv">$serviceContainer</span> <span class="o">=</span> <span class="nx">ServiceContainer</span><span class="o">::</span><span class="na">getInstance</span><span class="p">();</span>
|
|
|
-
|
|
|
- <span class="nv">$procedureLoader</span> <span class="o">=</span> <span class="nv">$serviceContainer</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'procedure_loader_factory'</span><span class="p">)</span>
|
|
|
- <span class="o">-></span><span class="na">createProcedureLoader</span><span class="p">(</span><span class="nv">$location</span><span class="p">);</span>
|
|
|
-
|
|
|
- <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>
|
|
|
- <span class="nv">$client</span><span class="o">-></span><span class="na">getProcedureLoader</span><span class="p">()</span><span class="o">-></span><span class="na">addLoader</span><span class="p">(</span><span class="nv">$procedureLoader</span><span class="p">);</span>
|
|
|
-
|
|
|
- <span class="nv">$request</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createRequest</span><span class="p">();</span>
|
|
|
- <span class="nv">$request</span><span class="o">-></span><span class="na">setType</span><span class="p">(</span><span class="s1">'my_procedure'</span><span class="p">);</span>
|
|
|
-
|
|
|
+ <span class="sd">/** </span>
|
|
|
+<span class="sd"> * @see JonnyW\PhantomJs\Message\Response </span>
|
|
|
+<span class="sd"> **/</span>
|
|
|
<span class="nv">$response</span> <span class="o">=</span> <span class="nv">$client</span><span class="o">-></span><span class="na">getMessageFactory</span><span class="p">()</span><span class="o">-></span><span class="na">createResponse</span><span class="p">();</span>
|
|
|
-
|
|
|
+
|
|
|
+ <span class="c1">// Send the request</span>
|
|
|
<span class="nv">$client</span><span class="o">-></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></div>
|
|
|
|
|
|
-<blockquote>
|
|
|
- <h4 id="troubleshooting">Troubleshooting</h4>
|
|
|
- <p>If you find that your script isn’t running or that you are receiving a status of ‘0’ back in the response, chances are you have a syntax error in you script. It pays to turn debugging on in the client <code>$client->debug(true)</code> which will then give you access to some log information through <code>$client->getLog()</code>.</p>
|
|
|
-</blockquote>
|
|
|
+<p>For more detailed examples see the <a href="/usage.html">usage</a> section, or to create your own custom scripts check out the <a href="/advanced.html">advanced</a> documentation.</p>
|
|
|
|
|
|
-<p>See more detailed information about <a href="http://jonnnnyw.github.io/php-phantomjs/debugging.html">debugging</a>.</p>
|
|
|
|
|
|
|
|
|
</div>
|
|
|
+
|
|
|
+ <div class="clearfix"></div>
|
|
|
+ <hr/>
|
|
|
|
|
|
-</article>
|
|
|
-
|
|
|
-<hr/>
|
|
|
+ <ul class="pager">
|
|
|
+
|
|
|
+ <li class="next">
|
|
|
+ <a href="/installation.html" title="Installation">Installation →</a>
|
|
|
+ </li>
|
|
|
+
|
|
|
+ </ul>
|
|
|
|
|
|
-<ul class="pager">
|
|
|
+</article>
|
|
|
|
|
|
-
|
|
|
- <li class="previous">
|
|
|
-
|
|
|
- <a href="http://jonnnnyw.github.io/php-phantomjs/page3">← Previous</a>
|
|
|
-
|
|
|
- </li>
|
|
|
-
|
|
|
|
|
|
-
|
|
|
- <li class="next">
|
|
|
- <a href="http://jonnnnyw.github.io/php-phantomjs/page5">Next →</a>
|
|
|
- </li>
|
|
|
-
|
|
|
|
|
|
-</ul>
|
|
|
|
|
|
|
|
|
|
|
|
@@ -500,9 +284,9 @@
|
|
|
</footer>
|
|
|
</div>
|
|
|
|
|
|
- <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/jquery/jquery.min.js"></script>
|
|
|
- <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/resources/bootstrap/js/bootstrap.min.js"></script>
|
|
|
- <script type="text/javascript" src="http://jonnnnyw.github.io/php-phantomjs/assets/js/app.js"></script>
|
|
|
+ <script type="text/javascript" src="/assets/resources/jquery/jquery.min.js"></script>
|
|
|
+ <script type="text/javascript" src="/assets/resources/bootstrap/js/bootstrap.min.js"></script>
|
|
|
+ <script type="text/javascript" src="/assets/js/app.js"></script>
|
|
|
|
|
|
</body>
|
|
|
</html>
|