1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Curl extension for Suricate |
9
|
|
|
* |
10
|
|
|
* @package Suricate |
11
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @property string $userAgent |
14
|
|
|
* @property int $timeout |
15
|
|
|
* @property string $proxyHost |
16
|
|
|
* @property int $proxyPort |
17
|
|
|
* @property string $referer |
18
|
|
|
* @property string $cookie |
19
|
|
|
* @property string $userAgent |
20
|
|
|
* @property mixed $postFields |
21
|
|
|
* @property string $login |
22
|
|
|
* @property string $password |
23
|
|
|
* @property array $headers |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
class Curl extends Service |
27
|
|
|
{ |
28
|
|
|
protected $parametersList = [ |
29
|
|
|
'userAgent', |
30
|
|
|
'timeout', |
31
|
|
|
'proxyHost', |
32
|
|
|
'proxyPort', |
33
|
|
|
'referer', |
34
|
|
|
'cookie', |
35
|
|
|
'userAgent', |
36
|
|
|
'postFields', |
37
|
|
|
'login', |
38
|
|
|
'password', |
39
|
|
|
'headers' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
private $request; |
43
|
|
|
private $response; |
44
|
|
|
private $responseData; |
45
|
|
|
private $errorMsg; |
46
|
|
|
private $errorCode; |
47
|
|
|
|
48
|
1 |
|
public function __construct() |
49
|
|
|
{ |
50
|
1 |
|
parent::__construct(); |
51
|
|
|
|
52
|
1 |
|
$this->request = new Request(); |
53
|
1 |
|
$this->response = new Request(); |
54
|
1 |
|
$this->headers = []; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function setUrl($url) |
58
|
|
|
{ |
59
|
1 |
|
$this->request->setUrl($url); |
60
|
|
|
|
61
|
1 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function getUrl() |
65
|
|
|
{ |
66
|
1 |
|
return $this->request->getUrl(); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function setMethod($method) |
70
|
|
|
{ |
71
|
1 |
|
$this->request->setMethod($method); |
72
|
|
|
|
73
|
1 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function setUserAgent($userAgent) |
77
|
|
|
{ |
78
|
1 |
|
$this->userAgent = $userAgent; |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function addHeader($headerLine) |
84
|
|
|
{ |
85
|
1 |
|
$headers = $this->headers; |
86
|
1 |
|
$headers[] = $headerLine; |
87
|
|
|
|
88
|
1 |
|
$this->headers = $headers; |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function send() |
94
|
|
|
{ |
95
|
|
|
$curlHandler = curl_init($this->request->getUrl()); |
96
|
|
|
|
97
|
|
|
if ($curlHandler === false) { |
98
|
|
|
throw new \Exception('Can\'t init curl'); |
99
|
|
|
} |
100
|
|
|
$curlOptions = $this->generateCurlOptions(); |
101
|
|
|
curl_setopt_array($curlHandler, $curlOptions); |
102
|
|
|
|
103
|
|
|
$curlResponse = curl_exec($curlHandler); |
104
|
|
|
if ($curlResponse === false) { |
105
|
|
|
$this->errorMsg = curl_error($curlHandler); |
106
|
|
|
$this->errorCode = curl_errno($curlHandler); |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
// No return transfer option |
111
|
|
|
if (is_bool($curlResponse)) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->responseData = curl_getinfo($curlHandler); |
116
|
|
|
$this->response->setUrl($this->responseData['url']); |
117
|
|
|
$redirectCount = curl_getinfo($curlHandler, CURLINFO_REDIRECT_COUNT); |
118
|
|
|
|
119
|
|
|
$splittedResponse = explode( |
120
|
|
|
"\r\n\r\n", |
121
|
|
|
$curlResponse, |
122
|
|
|
$redirectCount + 2 |
123
|
|
|
); |
124
|
|
|
$lastHeader = $splittedResponse[$redirectCount]; |
125
|
|
|
|
126
|
|
|
// get headers out of response |
127
|
|
|
$headers = explode("\n", trim($lastHeader)); |
128
|
|
|
array_shift($headers); |
129
|
|
|
|
130
|
|
|
foreach ($headers as $headerLine) { |
131
|
|
|
preg_match('|^([\d\w\s_-]*):(.*)|', $headerLine, $matches); |
132
|
|
|
if (isset($matches[1])) { |
133
|
|
|
$this->response->addHeader($matches[1], trim($matches[2])); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Reponse data |
138
|
|
|
$this->response->setHttpCode($this->responseData['http_code']); |
139
|
|
|
$this->response->setBody( |
140
|
|
|
substr($curlResponse, $this->responseData['header_size']) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function generateCurlOptions() |
147
|
|
|
{ |
148
|
|
|
$curlOptions = [ |
149
|
|
|
CURLOPT_RETURNTRANSFER => true, |
150
|
|
|
CURLOPT_HEADER => true, |
151
|
|
|
CURLINFO_HEADER_OUT => true, |
152
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
153
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
154
|
|
|
CURLOPT_SSL_VERIFYHOST => false |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
$parametersMapping = [ |
158
|
|
|
CURLOPT_CONNECTTIMEOUT => 'timeout', |
159
|
|
|
CURLOPT_PROXY => 'proxyHost', |
160
|
|
|
CURLOPT_PROXYPORT => 'proxyPort', |
161
|
|
|
CURLOPT_REFERER => 'referer', |
162
|
|
|
CURLOPT_COOKIE => 'cookie', |
163
|
|
|
CURLOPT_USERAGENT => 'userAgent', |
164
|
|
|
CURLOPT_HTTPHEADER => 'headers' |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
foreach ($parametersMapping as $curlKey => $optionKey) { |
168
|
|
|
if (($value = $this->getParameter($optionKey)) !== null) { |
169
|
|
|
$curlOptions[$curlKey] = $value; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// |
174
|
|
|
// Method management |
175
|
|
|
// |
176
|
|
|
if ($this->request->getMethod() == Request::HTTP_METHOD_GET) { |
177
|
|
|
$curlOptions[CURLOPT_HTTPGET] = true; |
178
|
|
|
} elseif ($this->request->getMethod() == Request::HTTP_METHOD_POST) { |
179
|
|
|
$curlOptions[CURLOPT_POST] = true; |
180
|
|
|
if ($this->getParameter('postFields') !== null) { |
181
|
|
|
$curlOptions[CURLOPT_POSTFIELDS] = $this->getParameter( |
182
|
|
|
'postFields' |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} elseif ($this->request->getMethod() == Request::HTTP_METHOD_PUT) { |
186
|
|
|
$curlOptions[CURLOPT_PUT] = true; |
187
|
|
|
} elseif ($this->request->getMethod() == Request::HTTP_METHOD_HEAD) { |
188
|
|
|
$curlOptions[CURLOPT_NOBODY] = true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $curlOptions; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getResponse() |
195
|
|
|
{ |
196
|
|
|
return $this->response; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getHeaders() |
200
|
|
|
{ |
201
|
|
|
return $this->response->getHeaders(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getHttpCode() |
205
|
|
|
{ |
206
|
|
|
return isset($this->responseData['http_code']) |
207
|
|
|
? $this->responseData['http_code'] |
208
|
|
|
: null; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getErrorMsg() |
212
|
|
|
{ |
213
|
|
|
return $this->errorMsg; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function getErrorCode() |
217
|
|
|
{ |
218
|
|
|
return $this->errorCode; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|