Ver Fonte

Merge pull request #7 from tuchk4/master

Added delay to open command
Jonny Wenmoth há 11 anos atrás
pai
commit
cb2e4a2fd9
2 ficheiros alterados com 75 adições e 6 exclusões
  1. 31 0
      README.md
  2. 44 6
      src/JonnyW/PhantomJs/Client.php

+ 31 - 0
README.md

@@ -64,6 +64,37 @@ if($response->getStatus() === 200) {
 }
 ```
 
+
+Request a URL with delay and output the reponse:
+
+```php
+<?php
+
+use JonnyW\PhantomJs\Client;
+
+$client = Client::getInstance();
+
+/** 
+ * @see JonnyW\PhantomJs\Message\Request 
+ **/
+$request = $client->getMessageFactory()->createRequest('GET', 'http://google.com');
+
+/** 
+ * @see JonnyW\PhantomJs\Message\Response 
+ **/
+$response = $client->getMessageFactory()->createResponse();
+
+// Send the request with delay in miliseconds
+$client->open($request, $response, $delay = 5000);
+
+if($response->getStatus() === 200) {
+	
+	// Dump the requested page content
+	echo $response->getContent();
+}
+```
+
+
 Request a URL, save a screen capture to provided location and return the response:
 
 ```php

+ 44 - 6
src/JonnyW/PhantomJs/Client.php

@@ -118,9 +118,15 @@ class Client implements ClientInterface
      * @param  ResponseInterface $response
      * @return ResponseInterface
      */
-    public function open(RequestInterface $request, ResponseInterface $response)
+    public function open(RequestInterface $request, ResponseInterface $response, $delay = 0)
     {
-        return $this->request($request, $response, $this->openCmd);
+        if ($delay) {
+            $cmd = sprintf($this->openCmdWithDelay, $delay);   
+        } else {
+            $cmd = $this->openCmd;
+        }
+
+        return $this->request($request, $response, $cmd);
     }
 
     /**
@@ -182,6 +188,7 @@ class Client implements ClientInterface
      */
     protected function request(RequestInterface $request, ResponseInterface $response, $cmd)
     {
+
         // Validate PhantomJS executable
         if (!file_exists($this->phantomJS) || !is_executable($this->phantomJS)) {
             throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $this->phantomJS));
@@ -200,6 +207,8 @@ class Client implements ClientInterface
                 $request->getBody(),
                 $cmd
             );
+            
+
 
             $script = $this->writeScript($data);
             $cmd  = escapeshellcmd(sprintf("%s %s", $this->phantomJS, $script));
@@ -314,10 +323,10 @@ class Client implements ClientInterface
 
         if (status === 'success') {
             %6\$s
-        }
-
-        console.log(JSON.stringify(response, undefined, 4));
-        phantom.exit();
+        } else {
+            console.log(JSON.stringify(response, undefined, 4));
+            phantom.exit();
+        }        
     });
 EOF;
 
@@ -334,6 +343,9 @@ EOF;
             response.content = page.evaluate(function () {
                 return document.getElementsByTagName('html')[0].innerHTML
             });
+
+            console.log(JSON.stringify(response, undefined, 4));
+            phantom.exit();
 EOF;
 
     /**
@@ -347,5 +359,31 @@ EOF;
             response.content = page.evaluate(function () {
                 return document.getElementsByTagName('html')[0].innerHTML
             });
+
+            console.log(JSON.stringify(response, undefined, 4));
+            phantom.exit();
+EOF;
+
+ /**
+     * PhantomJs page open
+     * command template
+     *
+     * @var string
+     */
+    protected $openCmdWithDelay = <<<EOF
+
+        window.setTimeout(function(){
+
+
+            response.content = page.evaluate(function () {
+                return document.getElementsByTagName('html')[0].innerHTML
+            });
+
+            
+            console.log(JSON.stringify(response, undefined, 4));
+            phantom.exit();            
+
+        }, %s);
+
 EOF;
 }