|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\GouttePhantomJs\Clients\PhantomJs; |
|
4
|
|
|
|
|
5
|
|
|
use JonnyW\PhantomJs\Client as PhantomJsBaseClient; |
|
6
|
|
|
use JonnyW\PhantomJs\Http\RequestInterface; |
|
7
|
|
|
use PhantomInstaller\PhantomBinary; |
|
8
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
9
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
|
10
|
|
|
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class PhantomJsClientBridge |
|
14
|
|
|
* @package ByTIC\GouttePhantomJs\Clients\PhantomJs |
|
15
|
|
|
*/ |
|
16
|
|
|
class ClientBridge implements HttpClientInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected $phantomJsClient = null; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array Default request options */ |
|
21
|
|
|
private $config; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* ClientBridge constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $config |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function __construct(array $config = []) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->configureDefaults($config); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
|
|
public function request(string $method, string $url, array $options = []): ResponseInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$client = $this->getPhantomJsClient(); |
|
39
|
|
|
$request = $this->createRequest($client, $method, $url, $options); |
|
|
|
|
|
|
40
|
|
|
$request = $this->applyConfig($request); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @see \JonnyW\PhantomJs\Http\Response |
|
44
|
|
|
**/ |
|
45
|
|
|
$response = $client->getMessageFactory()->createResponse(); |
|
46
|
|
|
|
|
47
|
|
|
// Send the request |
|
48
|
|
|
$client->send($request, $response); |
|
49
|
|
|
|
|
50
|
|
|
return ResponseFormatter::format($response); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function requestAsync($method, $uri = '', array $options = []) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function send(RequestInterface $request, array $options = []) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function sendAsync(RequestInterface $request, array $options = []) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function stream($responses, float $timeout = null): ResponseStreamInterface |
|
78
|
|
|
{ |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritdoc |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getConfig($option = null) |
|
85
|
|
|
{ |
|
86
|
|
|
return $option === null |
|
87
|
|
|
? $this->config |
|
88
|
|
|
: (isset($this->config[$option]) ? $this->config[$option] : null); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Configures the default options for a client. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $config |
|
95
|
|
|
*/ |
|
96
|
1 |
|
protected function configureDefaults(array $config) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$defaults = $this->getConfigDefaults(); |
|
99
|
|
|
|
|
100
|
1 |
|
$this->config = $config + $defaults; |
|
101
|
1 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
1 |
|
protected function getConfigDefaults() |
|
107
|
|
|
{ |
|
108
|
|
|
return [ |
|
109
|
1 |
|
'request_delay' => false |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $option |
|
115
|
|
|
* @param $value |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setConfig($option, $value) |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->config[$option] = $value; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param PhantomJsBaseClient $client |
|
126
|
|
|
* @param $method |
|
127
|
|
|
* @param string $uri |
|
128
|
|
|
* @param array $parameters |
|
129
|
|
|
* |
|
130
|
|
|
* @return RequestInterface |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function createRequest($client, $method, $uri = '', array $parameters = []) |
|
133
|
|
|
{ |
|
134
|
|
|
/** |
|
135
|
|
|
* @see \JonnyW\PhantomJs\Http\Request |
|
136
|
|
|
**/ |
|
137
|
|
|
$request = $client->getMessageFactory()->createRequest($uri, $method); |
|
138
|
|
|
$request->addHeader( |
|
139
|
|
|
'User-Agent', |
|
140
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0' |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$request->addHeader( |
|
144
|
|
|
'Content-Type', |
|
145
|
|
|
'application/x-www-form-urlencoded' |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
if (isset($parameters['form_params'])) { |
|
149
|
|
|
$request->setRequestData($parameters['form_params']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $request; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param RequestInterface $request |
|
157
|
|
|
* |
|
158
|
|
|
* @return RequestInterface |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function applyConfig($request) |
|
161
|
|
|
{ |
|
162
|
|
|
$requestDelay = $this->getConfig('request_delay'); |
|
163
|
|
|
if ($requestDelay > 0) { |
|
164
|
|
|
$request->setDelay($requestDelay); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $request; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return PhantomJsBaseClient|null |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getPhantomJsClient() |
|
175
|
|
|
{ |
|
176
|
|
|
if ($this->phantomJsClient === null) { |
|
177
|
|
|
$this->phantomJsClient = PhantomJsBaseClient::getInstance(); |
|
178
|
|
|
$this->phantomJsClient->getEngine()->setPath(PhantomBinary::getBin()); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->phantomJsClient; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: