1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Http Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Http; |
10
|
|
|
|
11
|
|
|
use Joomla\Uri\Uri; |
12
|
|
|
use Joomla\Uri\UriInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HTTP client class. |
16
|
|
|
* |
17
|
|
|
* @since 1.0 |
18
|
|
|
*/ |
19
|
|
|
class Http |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Options for the HTTP client. |
23
|
|
|
* |
24
|
|
|
* @var array|\ArrayAccess |
25
|
|
|
* @since 1.0 |
26
|
|
|
*/ |
27
|
|
|
protected $options; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The HTTP transport object to use in sending HTTP requests. |
31
|
|
|
* |
32
|
|
|
* @var TransportInterface |
33
|
|
|
* @since 1.0 |
34
|
|
|
*/ |
35
|
|
|
protected $transport; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param array|\ArrayAccess $options Client options array. If the registry contains any headers.* elements, |
41
|
|
|
* these will be added to the request headers. |
42
|
|
|
* @param TransportInterface $transport The HTTP transport object. |
43
|
|
|
* |
44
|
|
|
* @since 1.0 |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function __construct($options = array(), TransportInterface $transport = null) |
48
|
|
|
{ |
49
|
|
|
if (!is_array($options) && !($options instanceof \ArrayAccess)) |
50
|
|
|
{ |
51
|
|
|
throw new \InvalidArgumentException( |
52
|
|
|
'The options param must be an array or implement the ArrayAccess interface.' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->options = $options; |
57
|
|
|
|
58
|
|
|
if (!isset($transport)) |
59
|
|
|
{ |
60
|
|
|
$transport = HttpFactory::getAvailableDriver($this->options); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Ensure the transport is a TransportInterface instance or bail out |
64
|
|
|
if (!($transport instanceof TransportInterface)) |
65
|
|
|
{ |
66
|
|
|
throw new \InvalidArgumentException('A valid TransportInterface object was not set.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->transport = $transport; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get an option from the HTTP client. |
74
|
|
|
* |
75
|
|
|
* @param string $key The name of the option to get. |
76
|
|
|
* |
77
|
|
|
* @return mixed The option value. |
78
|
|
|
* |
79
|
|
|
* @since 1.0 |
80
|
|
|
*/ |
81
|
|
|
public function getOption($key) |
82
|
|
|
{ |
83
|
|
|
return isset($this->options[$key]) ? $this->options[$key] : null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set an option for the HTTP client. |
88
|
|
|
* |
89
|
|
|
* @param string $key The name of the option to set. |
90
|
|
|
* @param mixed $value The option value to set. |
91
|
|
|
* |
92
|
|
|
* @return Http This object for method chaining. |
93
|
|
|
* |
94
|
|
|
* @since 1.0 |
95
|
|
|
*/ |
96
|
|
|
public function setOption($key, $value) |
97
|
|
|
{ |
98
|
|
|
$this->options[$key] = $value; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Method to send the OPTIONS command to the server. |
105
|
|
|
* |
106
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
107
|
|
|
* @param array $headers An array of request headers to send with the request. |
108
|
|
|
* @param integer $timeout Read timeout in seconds. |
109
|
|
|
* |
110
|
|
|
* @return Response |
111
|
|
|
* |
112
|
|
|
* @since 1.0 |
113
|
|
|
*/ |
114
|
|
|
public function options($url, array $headers = array(), $timeout = null) |
115
|
|
|
{ |
116
|
|
|
return $this->makeTransportRequest('OPTIONS', $url, null, $headers, $timeout); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Method to send the HEAD command to the server. |
121
|
|
|
* |
122
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
123
|
|
|
* @param array $headers An array of request headers to send with the request. |
124
|
|
|
* @param integer $timeout Read timeout in seconds. |
125
|
|
|
* |
126
|
|
|
* @return Response |
127
|
|
|
* |
128
|
|
|
* @since 1.0 |
129
|
|
|
*/ |
130
|
|
|
public function head($url, array $headers = array(), $timeout = null) |
131
|
|
|
{ |
132
|
|
|
return $this->makeTransportRequest('HEAD', $url, null, $headers, $timeout); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Method to send the GET command to the server. |
137
|
|
|
* |
138
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
139
|
|
|
* @param array $headers An array of request headers to send with the request. |
140
|
|
|
* @param integer $timeout Read timeout in seconds. |
141
|
|
|
* |
142
|
|
|
* @return Response |
143
|
|
|
* |
144
|
|
|
* @since 1.0 |
145
|
|
|
*/ |
146
|
|
|
public function get($url, array $headers = array(), $timeout = null) |
147
|
|
|
{ |
148
|
|
|
return $this->makeTransportRequest('GET', $url, null, $headers, $timeout); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Method to send the POST command to the server. |
153
|
|
|
* |
154
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
155
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
156
|
|
|
* @param array $headers An array of request headers to send with the request. |
157
|
|
|
* @param integer $timeout Read timeout in seconds. |
158
|
|
|
* |
159
|
|
|
* @return Response |
160
|
|
|
* |
161
|
|
|
* @since 1.0 |
162
|
|
|
*/ |
163
|
|
|
public function post($url, $data, array $headers = array(), $timeout = null) |
164
|
|
|
{ |
165
|
|
|
return $this->makeTransportRequest('POST', $url, $data, $headers, $timeout); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Method to send the PUT command to the server. |
170
|
|
|
* |
171
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
172
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
173
|
|
|
* @param array $headers An array of request headers to send with the request. |
174
|
|
|
* @param integer $timeout Read timeout in seconds. |
175
|
|
|
* |
176
|
|
|
* @return Response |
177
|
|
|
* |
178
|
|
|
* @since 1.0 |
179
|
|
|
*/ |
180
|
|
|
public function put($url, $data, array $headers = array(), $timeout = null) |
181
|
|
|
{ |
182
|
|
|
return $this->makeTransportRequest('PUT', $url, $data, $headers, $timeout); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Method to send the DELETE command to the server. |
187
|
|
|
* |
188
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
189
|
|
|
* @param array $headers An array of request headers to send with the request. |
190
|
|
|
* @param integer $timeout Read timeout in seconds. |
191
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
192
|
|
|
* |
193
|
|
|
* @return Response |
194
|
|
|
* |
195
|
|
|
* @since 1.0 |
196
|
|
|
*/ |
197
|
|
|
public function delete($url, array $headers = array(), $timeout = null, $data = null) |
198
|
|
|
{ |
199
|
|
|
return $this->makeTransportRequest('DELETE', $url, $data, $headers, $timeout); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Method to send the TRACE command to the server. |
204
|
|
|
* |
205
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
206
|
|
|
* @param array $headers An array of request headers to send with the request. |
207
|
|
|
* @param integer $timeout Read timeout in seconds. |
208
|
|
|
* |
209
|
|
|
* @return Response |
210
|
|
|
* |
211
|
|
|
* @since 1.0 |
212
|
|
|
*/ |
213
|
|
|
public function trace($url, array $headers = array(), $timeout = null) |
214
|
|
|
{ |
215
|
|
|
return $this->makeTransportRequest('TRACE', $url, null, $headers, $timeout); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Method to send the PATCH command to the server. |
220
|
|
|
* |
221
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
222
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
223
|
|
|
* @param array $headers An array of request headers to send with the request. |
224
|
|
|
* @param integer $timeout Read timeout in seconds. |
225
|
|
|
* |
226
|
|
|
* @return Response |
227
|
|
|
* |
228
|
|
|
* @since 1.0 |
229
|
|
|
*/ |
230
|
|
|
public function patch($url, $data, array $headers = array(), $timeout = null) |
231
|
|
|
{ |
232
|
|
|
return $this->makeTransportRequest('PATCH', $url, $data, $headers, $timeout); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Send a request to the server and return a Response object with the response. |
237
|
|
|
* |
238
|
|
|
* @param string $method The HTTP method for sending the request. |
239
|
|
|
* @param string|UriInterface $url The URI to the resource to request. |
240
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
241
|
|
|
* @param array $headers An array of request headers to send with the request. |
242
|
|
|
* @param integer $timeout Read timeout in seconds. |
243
|
|
|
* |
244
|
|
|
* @return Response |
245
|
|
|
* |
246
|
|
|
* @since 1.0 |
247
|
|
|
* @throws \InvalidArgumentException |
248
|
|
|
*/ |
249
|
|
|
protected function makeTransportRequest($method, $url, $data = null, array $headers = array(), $timeout = null) |
250
|
|
|
{ |
251
|
|
|
// Look for headers set in the options. |
252
|
|
|
if (isset($this->options['headers'])) |
253
|
|
|
{ |
254
|
|
|
$temp = (array) $this->options['headers']; |
255
|
|
|
|
256
|
|
|
foreach ($temp as $key => $val) |
257
|
|
|
{ |
258
|
|
|
if (!isset($headers[$key])) |
259
|
|
|
{ |
260
|
|
|
$headers[$key] = $val; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// Look for timeout set in the options. |
266
|
|
|
if ($timeout === null && isset($this->options['timeout'])) |
267
|
|
|
{ |
268
|
|
|
$timeout = $this->options['timeout']; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$userAgent = isset($this->options['userAgent']) ? $this->options['userAgent'] : null; |
272
|
|
|
|
273
|
|
|
// Convert to a Uri object if we were given a string |
274
|
|
|
if (is_string($url)) |
275
|
|
|
{ |
276
|
|
|
$url = new Uri($url); |
277
|
|
|
} |
278
|
|
|
elseif (!($url instanceof UriInterface)) |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
throw new \InvalidArgumentException(sprintf('A string or UriInterface object must be provided, a "%s" was provided.', gettype($url))); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $this->transport->request($method, $url, $data, $headers, $timeout, $userAgent); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.