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