Skip to content

Commit cb2e4a2

Browse files
committed
Merge pull request jonnnnyw#7 from tuchk4/master
Added delay to open command
2 parents 40009bd + 8d13790 commit cb2e4a2

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,37 @@ if($response->getStatus() === 200) {
6464
}
6565
```
6666

67+
68+
Request a URL with delay and output the reponse:
69+
70+
```php
71+
<?php
72+
73+
use JonnyW\PhantomJs\Client;
74+
75+
$client = Client::getInstance();
76+
77+
/**
78+
* @see JonnyW\PhantomJs\Message\Request
79+
**/
80+
$request = $client->getMessageFactory()->createRequest('GET', 'http://google.com');
81+
82+
/**
83+
* @see JonnyW\PhantomJs\Message\Response
84+
**/
85+
$response = $client->getMessageFactory()->createResponse();
86+
87+
// Send the request with delay in miliseconds
88+
$client->open($request, $response, $delay = 5000);
89+
90+
if($response->getStatus() === 200) {
91+
92+
// Dump the requested page content
93+
echo $response->getContent();
94+
}
95+
```
96+
97+
6798
Request a URL, save a screen capture to provided location and return the response:
6899

69100
```php

src/JonnyW/PhantomJs/Client.php

+44-6
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ public function send(RequestInterface $request, ResponseInterface $response, $fi
118118
* @param ResponseInterface $response
119119
* @return ResponseInterface
120120
*/
121-
public function open(RequestInterface $request, ResponseInterface $response)
121+
public function open(RequestInterface $request, ResponseInterface $response, $delay = 0)
122122
{
123-
return $this->request($request, $response, $this->openCmd);
123+
if ($delay) {
124+
$cmd = sprintf($this->openCmdWithDelay, $delay);
125+
} else {
126+
$cmd = $this->openCmd;
127+
}
128+
129+
return $this->request($request, $response, $cmd);
124130
}
125131

126132
/**
@@ -182,6 +188,7 @@ public function setTimeout($period)
182188
*/
183189
protected function request(RequestInterface $request, ResponseInterface $response, $cmd)
184190
{
191+
185192
// Validate PhantomJS executable
186193
if (!file_exists($this->phantomJS) || !is_executable($this->phantomJS)) {
187194
throw new NoPhantomJsException(sprintf('PhantomJs file does not exist or is not executable: %s', $this->phantomJS));
@@ -200,6 +207,8 @@ protected function request(RequestInterface $request, ResponseInterface $respons
200207
$request->getBody(),
201208
$cmd
202209
);
210+
211+
203212

204213
$script = $this->writeScript($data);
205214
$cmd = escapeshellcmd(sprintf("%s %s", $this->phantomJS, $script));
@@ -314,10 +323,10 @@ protected function parse($data)
314323
315324
if (status === 'success') {
316325
%6\$s
317-
}
318-
319-
console.log(JSON.stringify(response, undefined, 4));
320-
phantom.exit();
326+
} else {
327+
console.log(JSON.stringify(response, undefined, 4));
328+
phantom.exit();
329+
}
321330
});
322331
EOF;
323332

@@ -334,6 +343,9 @@ protected function parse($data)
334343
response.content = page.evaluate(function () {
335344
return document.getElementsByTagName('html')[0].innerHTML
336345
});
346+
347+
console.log(JSON.stringify(response, undefined, 4));
348+
phantom.exit();
337349
EOF;
338350

339351
/**
@@ -347,5 +359,31 @@ protected function parse($data)
347359
response.content = page.evaluate(function () {
348360
return document.getElementsByTagName('html')[0].innerHTML
349361
});
362+
363+
console.log(JSON.stringify(response, undefined, 4));
364+
phantom.exit();
365+
EOF;
366+
367+
/**
368+
* PhantomJs page open
369+
* command template
370+
*
371+
* @var string
372+
*/
373+
protected $openCmdWithDelay = <<<EOF
374+
375+
window.setTimeout(function(){
376+
377+
378+
response.content = page.evaluate(function () {
379+
return document.getElementsByTagName('html')[0].innerHTML
380+
});
381+
382+
383+
console.log(JSON.stringify(response, undefined, 4));
384+
phantom.exit();
385+
386+
}, %s);
387+
350388
EOF;
351389
}

0 commit comments

Comments
 (0)