1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/HttpClient |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\HttpClient\Client; |
9
|
|
|
|
10
|
|
|
use Brownie\HttpClient\Cookie\Cookie; |
11
|
|
|
use Brownie\HttpClient\Cookie\CookieList; |
12
|
|
|
use Brownie\HttpClient\Header\Header; |
13
|
|
|
use Brownie\HttpClient\Header\HeaderList; |
14
|
|
|
use Brownie\HttpClient\Request; |
15
|
|
|
use Brownie\HttpClient\Response; |
16
|
|
|
use Brownie\HttpClient\Client; |
17
|
|
|
use Brownie\HttpClient\Exception\ClientException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* API(Adapter) for using CURL functions in HTTP requests. |
21
|
|
|
*/ |
22
|
|
|
class CurlAdapter implements Client |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* API CURL functions. |
27
|
|
|
* |
28
|
|
|
* @var CurlAdaptee |
29
|
|
|
*/ |
30
|
|
|
private $adaptee; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets incoming data. |
34
|
|
|
* |
35
|
|
|
* @param CurlAdaptee $adaptee API CURL functions. |
36
|
|
|
*/ |
37
|
7 |
|
public function __construct(CurlAdaptee $adaptee) |
38
|
|
|
{ |
39
|
7 |
|
$this->setAdaptee($adaptee); |
40
|
7 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets API CURL functions wrapper. |
44
|
|
|
* Returns the current object. |
45
|
|
|
* |
46
|
|
|
* @param CurlAdaptee $adaptee |
47
|
|
|
* |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
7 |
|
private function setAdaptee(CurlAdaptee $adaptee) |
51
|
|
|
{ |
52
|
7 |
|
$this->adaptee = $adaptee; |
53
|
7 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns API CURL functions wrapper. |
58
|
|
|
* |
59
|
|
|
* @return CurlAdaptee |
60
|
|
|
*/ |
61
|
7 |
|
private function getAdaptee() |
62
|
|
|
{ |
63
|
7 |
|
return $this->adaptee; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Performs a network request. |
68
|
|
|
* Returns the response. |
69
|
|
|
* |
70
|
|
|
* @param Request $request HTTP request params. |
71
|
|
|
* |
72
|
|
|
* @throws ClientException |
73
|
|
|
* |
74
|
|
|
* @return Response |
75
|
|
|
*/ |
76
|
7 |
|
public function httpRequest(Request $request) |
77
|
|
|
{ |
78
|
7 |
|
$curl = $this->getCurlClient($request); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Executes a network resource request. |
82
|
|
|
*/ |
83
|
7 |
|
$responseBody = $this->getAdaptee()->exec($curl); |
84
|
|
|
|
85
|
7 |
|
$httpCode = $this->getAdaptee()->getinfo($curl, CURLINFO_HTTP_CODE); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Network error checking. |
89
|
|
|
*/ |
90
|
7 |
|
if ((0 != $this->getAdaptee()->errno($curl)) || !is_string($responseBody)) { |
91
|
1 |
|
throw new ClientException($this->getAdaptee()->error($curl)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the execution time of the request. |
96
|
|
|
*/ |
97
|
6 |
|
$runtime = $this->getAdaptee()->getinfo($curl, CURLINFO_TOTAL_TIME); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the HTTP headers and the body separately. |
101
|
|
|
*/ |
102
|
6 |
|
$headerSize = $this->getAdaptee()->getinfo($curl, CURLINFO_HEADER_SIZE); |
103
|
6 |
|
$body = substr($responseBody, $headerSize); |
104
|
|
|
|
105
|
6 |
|
$this->getAdaptee()->close($curl); |
106
|
|
|
|
107
|
6 |
|
$httpHeadersString = substr($responseBody, 0, $headerSize); |
108
|
|
|
|
109
|
6 |
|
$response = new Response(); |
110
|
|
|
return $response |
111
|
6 |
|
->setBody($body) |
112
|
6 |
|
->setHttpCode($httpCode) |
113
|
6 |
|
->setRuntime($runtime) |
114
|
6 |
|
->setHttpHeaderList(new HeaderList($httpHeadersString)) |
115
|
6 |
|
->setHttpCookieList(new CookieList($httpHeadersString)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates and returns a CURL resource. |
120
|
|
|
* |
121
|
|
|
* @param Request $request HTTP request params. |
122
|
|
|
* |
123
|
|
|
* @return resource |
124
|
|
|
*/ |
125
|
7 |
|
private function getCurlClient(Request $request) |
126
|
|
|
{ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Build URL. |
130
|
|
|
*/ |
131
|
7 |
|
$url = $this->getUrl($request); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Initializing CURL. |
135
|
|
|
*/ |
136
|
7 |
|
$curl = $this->getAdaptee()->init($url); |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* CURL setting. |
140
|
|
|
*/ |
141
|
7 |
|
$this->setBaseOpt($curl, $request, $url); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Configuring HTTP headers. |
145
|
|
|
*/ |
146
|
7 |
|
$this->setHedaers($curl, $request); |
147
|
|
|
|
148
|
7 |
|
return $curl; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Generates and returns URL. |
153
|
|
|
* |
154
|
|
|
* @param Request $request HTTP request params. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
7 |
|
private function getUrl(Request $request) |
159
|
|
|
{ |
160
|
7 |
|
$url = $request->getUrl(); |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Adds GET parameters. |
164
|
|
|
*/ |
165
|
7 |
|
$params = $request->getParams(); |
166
|
7 |
|
if ((Request::HTTP_METHOD_GET == $request->getMethod()) && !empty($params)) { |
167
|
6 |
|
$url .= '?' . http_build_query($params); |
168
|
|
|
} |
169
|
|
|
|
170
|
7 |
|
return $url; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sets the basic options. |
175
|
|
|
* |
176
|
|
|
* @param resource $curl CURL resource. |
177
|
|
|
* @param Request $request HTTP request params. |
178
|
|
|
* @param string $url Request URL. |
179
|
|
|
*/ |
180
|
7 |
|
private function setBaseOpt($curl, Request $request, $url) |
181
|
|
|
{ |
182
|
|
|
$this |
183
|
7 |
|
->getAdaptee() |
184
|
7 |
|
->setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod()) |
185
|
7 |
|
->setopt($curl, CURLOPT_TIMEOUT, $request->getTimeOut()) |
186
|
7 |
|
->setopt($curl, CURLOPT_NOPROGRESS, true) |
187
|
7 |
|
->setopt($curl, CURLOPT_RETURNTRANSFER, true) |
188
|
7 |
|
->setopt($curl, CURLOPT_URL, $url) |
189
|
7 |
|
->setopt($curl, CURLOPT_HEADER, true); |
190
|
|
|
|
191
|
7 |
|
if ($this->isPostParams($request)) { |
192
|
|
|
$this |
193
|
1 |
|
->getAdaptee() |
194
|
1 |
|
->setopt($curl, CURLOPT_POST, true); |
195
|
1 |
|
$params = $request->getParams(); |
196
|
1 |
|
if (!empty($params)) { |
197
|
1 |
|
$params = http_build_query($params); |
198
|
1 |
|
$request->setBody($params); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this |
203
|
7 |
|
->getAdaptee() |
204
|
7 |
|
->setopt($curl, CURLOPT_POSTFIELDS, $request->getBody()); |
205
|
|
|
|
206
|
7 |
|
$this->triggerDisableSSLCertificateValidation($curl, $request); |
207
|
|
|
|
208
|
7 |
|
$this->triggerAuthentication($curl, $request); |
209
|
7 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the POST character of the data. |
213
|
|
|
* |
214
|
|
|
* @param Request $request HTTP request params. |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
7 |
|
private function isPostParams(Request $request) |
219
|
|
|
{ |
220
|
7 |
|
return (Request::HTTP_METHOD_POST == $request->getMethod()) || |
221
|
7 |
|
(Request::HTTP_METHOD_PUT == $request->getMethod()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Controlling verification of SSL certificates. |
226
|
|
|
* |
227
|
|
|
* @param resource $curl CURL resource. |
228
|
|
|
* @param Request $request HTTP request params. |
229
|
|
|
*/ |
230
|
7 |
|
private function triggerDisableSSLCertificateValidation($curl, Request $request) |
231
|
|
|
{ |
232
|
7 |
|
if ($request->isDisableSSLValidation()) { |
233
|
|
|
/** |
234
|
|
|
* Disable SSL validation. |
235
|
|
|
*/ |
236
|
|
|
$this |
237
|
2 |
|
->getAdaptee() |
238
|
2 |
|
->setopt($curl, CURLOPT_SSL_VERIFYPEER, false) |
239
|
2 |
|
->setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
240
|
|
|
} |
241
|
7 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Controlling request authentication. |
245
|
|
|
* |
246
|
|
|
* @param resource $curl CURL resource. |
247
|
|
|
* @param Request $request HTTP request params. |
248
|
|
|
*/ |
249
|
7 |
|
private function triggerAuthentication($curl, Request $request) |
250
|
|
|
{ |
251
|
7 |
|
$authentication = $request->getAuthentication(); |
252
|
7 |
|
if (!empty($authentication)) { |
253
|
|
|
/** |
254
|
|
|
* Enable authentication. |
255
|
|
|
*/ |
256
|
|
|
$this |
257
|
7 |
|
->getAdaptee() |
258
|
7 |
|
->setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY) |
259
|
7 |
|
->setopt($curl, CURLOPT_USERPWD, $authentication); |
260
|
|
|
} |
261
|
7 |
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Sets HTTP headers. |
265
|
|
|
* |
266
|
|
|
* @param resource $curl CURL resource. |
267
|
|
|
* @param Request $request HTTP request params. |
268
|
|
|
*/ |
269
|
7 |
|
private function setHedaers($curl, Request $request) |
270
|
|
|
{ |
271
|
|
|
$headers = array( |
272
|
7 |
|
'Connection: close', |
273
|
7 |
|
'Accept-Ranges: bytes', |
274
|
7 |
|
'Content-Length: ' . strlen($request->getBody()), |
275
|
7 |
|
'Accept: ' . $request->getBodyFormat() . ',*/*', |
276
|
7 |
|
'User-Agent: ' . $this->getAdaptee()->getAgentString(), |
277
|
|
|
); |
278
|
|
|
|
279
|
7 |
|
if (!$this->isPostParams($request)) { |
280
|
6 |
|
$headers[] = 'Content-Type: ' . $request->getBodyFormat() . '; charset=utf-8'; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @var Header $header Header. |
285
|
|
|
*/ |
286
|
7 |
|
foreach ($request->getHeaders() as $header) { |
287
|
7 |
|
$headers[] = $header->toString(); |
288
|
|
|
} |
289
|
|
|
/** |
290
|
|
|
* @var Cookie $cookie Cookie. |
291
|
|
|
*/ |
292
|
7 |
|
$cookies = array(); |
293
|
7 |
|
foreach ($request->getCookies() as $cookie) { |
294
|
7 |
|
$cookies[] = $cookie->toString(); |
295
|
|
|
} |
296
|
7 |
|
if (!empty($cookies)) { |
297
|
7 |
|
$headers[] = 'Cookie: ' . implode('; ', $cookies); |
298
|
|
|
} |
299
|
7 |
|
$this->getAdaptee()->setopt($curl, CURLOPT_HTTPHEADER, $headers); |
300
|
7 |
|
} |
301
|
|
|
} |
302
|
|
|
|