This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
client.php
271 lines (221 loc) · 5.61 KB
/
client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* Spoon Library
*
* This source file is part of the Spoon Library. More information,
* documentation and tutorials can be found @ http://www.spoon-library.com
*
* @package spoon
* @subpackage rest
*
*
* @author Davy Hellemans <davy@spoon-library.com>
* @since 1.1.2
*/
/**
* This base class provides all the methods used by a REST-client.
*
* @package spoon
* @subpackage rest
*
*
* @author Tijs Verkoyen <tijs@spoon-library.com>
* @author Bram Van Damme <bramus@bram.us>
* @since 1.1.1
*/
class SpoonRESTClient
{
/**
* The headers
*
* @var array
*/
private $headers = array();
/**
* The port
*
* @var int
*/
private $port = 80;
/**
* The timeout in seconds
*
* @var int
*/
private $timeout = 10;
/**
* The user-agent
*
* @var string
*/
private $userAgent;
/**
* Make the call.
*
* @return string
* @param string $url The url to call.
* @param array[optional] $parameters The parameters to pass.
* @param string[optional] $method The HTTP-method to use, possible values are: GET, POST.
* @param array[optional] $cURLOptions Optional extra cURL-options.
*/
public function execute($url, array $parameters = null, $method = 'GET', array $cURLOptions = null)
{
// check if curl is available
if(!function_exists('curl_init')) throw new SpoonFileException('This method requires cURL (http://php.net/curl), it seems like the extension isn\'t installed.');
// init var
$allowedMethods = array('GET', 'POST', 'DELETE');
// redefine
$url = (string) $url;
$parameters = (array) $parameters;
$method = strtoupper((string) $method);
// validate
if(!in_array($method, $allowedMethods)) throw new SpoonRESTException('Invalid method (' . $method . '). Possible methods are: ' . implode(', ', $allowedMethods) . '.');
// init curl options
$options[CURLOPT_PORT] = $this->getPort();
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
$options[CURLOPT_TIMEOUT] = $this->getTimeout();
$options[CURLOPT_RETURNTRANSFER] = true;
// any extra curl options provided?
if($cURLOptions !== null)
{
// loop the extra options, and set 'm
foreach($cURLOptions as $key => $value) $options[$key] = $value;
}
// set headers
$headers = $this->getCustomHeaders();
if(!empty($headers)) $options[CURLOPT_HTTPHEADER] = $headers;
// specific when using GET
if($method == 'GET')
{
// init var
$queryString = '';
// loop parameters and append them to the url
foreach($parameters as $key => $value) $queryString .= '&' . $key . '=' . urlencode($value);
// cleanup
$queryString = trim($queryString, '&');
// is there really a querystring
if($queryString != '')
{
// find ? in url
if(strpos($url, '?') > 0) $url .= '&' . $queryString;
else $url .= '?' . $queryString;
}
}
// specific when using POST
if($method == 'POST' || $method == 'DELETE')
{
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $parameters;
}
// custom requests
if($method === 'DELETE')
{
$options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
}
// init curl
$curl = curl_init($url);
// set options
curl_setopt_array($curl, $options);
// execute
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
// fetch errors
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
// close curl
curl_close($curl);
// validate errors
if($errorNumber != 0) throw new SpoonRESTException('An error occured with the following message: (' . $errorNumber . ')' . $errorMessage . '.');
// validate headers
if($headers['http_code'] != 200) throw new SpoonRESTException('Invalid headers, a header with status-code ' . $headers['http_code'] . ' was returned.');
// return the response
return (string) $response;
}
/**
* Get the headers.
*
* @return array
*/
public function getCustomHeaders()
{
return $this->headers;
}
/**
* Get the port that will be used.
*
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Get the timeout in seconds that will be used.
*
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Get the user-agent that will be used. Keep in mind that a spoon header will be prepended.
*
* @return string
*/
public function getUserAgent()
{
// prepend SpoonHeader
$userAgent = 'Spoon ' . SPOON_VERSION . '/';
$userAgent .= ($this->userAgent === null) ? 'SpoonRESTClient' : $this->userAgent;
// return
return $userAgent;
}
/**
* Add custom headers that will be sent with each request.
*
* @param array $headers The header, passed as key-value pairs.
*/
public function setCustomHeader(array $headers)
{
foreach($headers as $name => $value) $this->headers[(string) $name] = (string) $value;
}
/**
* Set the port for the REST-server, default is 80.
*
* @param int $port The port to connect on.
*/
public function setPort($port)
{
$this->port = (int) $port;
}
/**
* Set timeout.
*
* @param int $seconds The maximum number of seconds that the operation can last.
*/
public function setTimeout($seconds)
{
$this->timeout = (int) $seconds;
}
/**
* Set a custom user-agent.
*
* @param string $userAgent The UserAgent that will be used. It will look like "Spoon <Spoon version>/<your useragent>".
*/
public function setUserAgent($userAgent)
{
$this->userAgent = (string) $userAgent;
}
}
/**
* This exception is used to handle REST related exceptions.
*
* @package spoon
* @subpackage rest
*
*
* @author Tijs Verkoyen <tijs@spoon-library.com>
* @since 1.1.1
*/
class SpoonRESTException extends SpoonException {}