-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestHelper.php
233 lines (203 loc) · 5.52 KB
/
RestHelper.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
<?php
/**
* User: Mario Galan
* Date: 10/29/13
* Time: 5:48 PM
*/
class RestHelper {
/**
* Default response format. Usually will be XML or JSON
*/
const DEFAULT_RESPONSE_FORMAT = 'XML';
/**
*
*/
const HTTP_METHOD_GET = 'GET';
/**
*
*/
const HTTP_METHOD_POST = 'POST';
/**
*
*/
const HTTP_METHOD_PUT = 'PUT';
/**
*
*/
const HTTP_METHOD_DELETE = 'DELETE';
/**
*
*/
const HTTP_METHOD_PATCH = 'PATCH';
/**
* Port to use on the call
*/
const DEFAULT_PORT = 80;
/**
* Set to TRUE to make curl output all the debug info
*/
const DEBUG_MODE = FALSE;
/**
* @var string
*/
private $response_format;
/**
* @var
*/
private $url;
/**
* @var
*/
private $endpoint;
/**
* @var int
*/
private $port;
/**
* @param $url
* @param $endpoint
* @param string $response_format
* @param int $port
*/
public function __construct($url, $endpoint, $response_format = self::DEFAULT_RESPONSE_FORMAT, $port = self::DEFAULT_PORT) {
$this->url = $url;
$this->endpoint = $endpoint;
$this->response_format = $response_format;
$this->port = $port;
}
/**
* Converts a raw response to an array of strings.
*
* @param $raw_response
*
* @internal param string $desired_format
*
* @return object
*/
private function transformResponse($raw_response) {
$output_response = '';
switch ($this->response_format) {
case 'XML':
// This hack is needed for converting the format returned by simplexml_load_string to an object with
// properties
$output_response = json_decode(json_encode(simplexml_load_string($raw_response)));
break;
case 'JSON':
$output_response = json_decode($raw_response);
break;
}
return $output_response;
}
/**
* Make a GET call.
*
* @param array $resource
* @param array $url_parameters
*
* @return mixed|SimpleXMLElement|string
*/
public function get(array $resource, array $url_parameters = array()) {
return $this->makeCall($resource, self::HTTP_METHOD_GET, $url_parameters);
}
/**
* Make a POST call.
*
* @param array $resource
* @param array $url_parameters
* @param array $post_data
*
* @return mixed|SimpleXMLElement|string
*/
public function post(array $resource, array $url_parameters = array(), array $post_data = array()) {
return $this->makeCall($resource, self::HTTP_METHOD_POST, $url_parameters, $post_data);
}
/**
* Make a PUT call.
* @param array $resource
* @param array $url_parameters
* @param array $post_data
*
* @return mixed|SimpleXMLElement|string
*/
public function put(array $resource, array $url_parameters = array(), array $post_data = array()) {
return $this->makeCall($resource, self::HTTP_METHOD_PUT, $url_parameters, $post_data);
}
/**
* Make a DELETE call.
*
* @param array $resource
* @param array $url_parameters
* @param array $post_data
*
* @return mixed|SimpleXMLElement|string
*/
public function delete(array $resource, array $url_parameters = array(), array $post_data = array()) {
return $this->makeCall($resource, self::HTTP_METHOD_DELETE, $url_parameters, $post_data);
}
/**
* Make a PATCH call.
*
* @param array $resource
* @param array $url_parameters
* @param array $post_data
*
* @return mixed|SimpleXMLElement|string
*/
public function patch(array $resource, array $url_parameters = array(), array $post_data = array()) {
return $this->makeCall($resource, self::HTTP_METHOD_PATCH, $url_parameters, $post_data);
}
/**
* Make a generic call.
*
* @param $resource
* @param $http_verb
* @param array $url_parameters
* @param array $post_data
*
* @internal param $desired_format
*
* @return mixed|SimpleXMLElement|string
*/
private function makeCall(array $resource, $http_verb, array $url_parameters = array(), array $post_data = array()) {
$resource = implode('/', $resource);
$complete_url = $this->url . '/' . $this->endpoint . '/' . $resource . '?' . http_build_query($url_parameters);
$ch = curl_init($complete_url);
// curl output to a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// We establish the port to work
curl_setopt($ch, CURLOPT_PORT, $this->port);
/*
* Note:
* Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded
* string will encode the data as application/x-www-form-urlencoded.
*/
$post_data = http_build_query($post_data);
if (self::DEBUG_MODE) {
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
}
switch ($http_verb) {
case self::HTTP_METHOD_GET:
break;
case self::HTTP_METHOD_POST:
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
break;
case self::HTTP_METHOD_PUT:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: PUT'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
break;
case self::HTTP_METHOD_DELETE:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case self::HTTP_METHOD_PATCH:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
break;
}
$response = curl_exec($ch);
curl_close($ch);
$response = self::transformResponse($response);
return $response;
}
}